Ruby objects, variables, and constants

Source: Internet
Author: User

The data for Ruby operations is mainly in parts: objects, classes, variables, constants.

Object

The basic unit of data represented in Ruby is called an object, and everything in Ruby is an object.
Common objects:

    • Numeric Objects
      2, 3.14, 5 and other objects representing numbers, in addition to the matrix, complex numbers, prime numbers, formulas of the object.
    • String Object
      "Hello World", "Hello" and other objects that represent text
    • Array object, hash object
      A collection of multi-degree systems that represent multiple data
    • Regular Expression Object
      The object of the regular expression-matching pattern
    • Time Object
      For example, "January 1, 2016 13 o'clock" and other objects that represent time
    • File Object
      objects that read and write to a file
    • Symbol Object
      An object that identifies a label for a method, such as a name.
class

Class is the kind of object, and the attribute of an object is determined by the class.

Object class
Numerical Numberic
String String
Array Array
Hash column Hash
Regular expressions Regexp
File File
Symbol Symbol

An object of a class is an instance of a class (instance)

variables

A variable is a business card for an object. There are four types of variables in Ruby:
- Local variables (locally variable)
- Global variables (globals variable)
- instance variable (instance variable)
- class variables (classes variable)

How variables are named:
Variable Type Naming Methods
Local variables Start with an English letter or an underscore (_)
Global variables Start with $
Instance variable Start with @
class variables Start with @@
Local variables and global variables

The valid range of local variables in a program is valid, and variable names declared somewhere in the program can also be used elsewhere. It corresponds to a global variable, and the global variable is a uniform variable anywhere in the program. Since the value of the global variable can be modified anywhere, using it in larger programs increases the complexity of the program unnecessarily, and reading programs and modifying programs can be cumbersome, hiding the fact that global variables are generally not used or used sparingly.

Constants

Constants begin with a thank-you letter, and constants can no longer be modified once they are assigned. For example, Ruby's running version (Ruby_version), run platform (ruby_platform), command line parameter array (ARGV), etc., are all predefined constants of Ruby.

Reserved Words

Reserved words cannot be used as variable names, otherwise they will be an error. Reserved words in Ruby:

Line
ENCODING FILE BEGIN END
=begin =end Alias and Begin
Defined? Do Else elsif END
Ensure False For If Inch
Module Next Nil Not Or
Redo Rescue Retry Return Self
Super Then True Undef Unless
Until When While Yield

Ruby objects, variables, and constants

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.