Ruby basic 3

Source: Internet
Author: User

Class
Everything in Ruby is an object, including a constant.
For example, you can use the. class Attribute to view the type of an object. You can see that the type of constant 1 is Fixnum, and 1 is just an instance of Fixnum.


In Ruby, classes start from class and end, and the conventions for the first letter of the class name are capitalized.
In Ruby, the method starts with def and ends with end. The conventions for the first letter of the method name are in lower case.
The first letter of a local variable in Ruby is in lower case.
The constructor name in Ruby is initialize.
The leading @ character of the member variable (instance variable) in Ruby, which is declared and initialized in initialize.
Attributes in Ruby are attr, attr_reader, attr_writer, and attr_accessor.
The $ identifier of the global variable in Ruby.
Constants (constants) in Ruby start with uppercase letters, and the conventions are all uppercase letters.


Initalize Method

When Ruby creates a new object, it always looks for a method named initialize and executes it. Therefore, we can simply add the default value to the Real Variable through an initialize method.


Class Person def initialize () puts "hello! "End # def initialize # () is omitted. # puts" hello! "# End # default value of the parameter # def initialize (param =" 1900lab ") # The parameter has the default value # puts" hello! "+ Param # end # variable length parameter def youInput (* names) puts" input # {names. join (",")}! "Endendp = Person. new () is the same as p = Person. new. No parameter () can be omitted.




Attribute aims to quickly generate a read/write Method


Attr: attr1, key
Attr_reader: attr1,: attr2
Attr_writer: attr1,: attr2
Attr_accessor: attr1,: attr2


Attr is generally followed by a symbolic parameter, and the second parameter is a Boolean parameter, used to indicate whether it is a symbolic parameter to generate a write method. The default value is false. Only the read method is generated, and no write method is generated.
Attr_reader is generally followed by a symbolic parameter, which defines one or more read-only attributes to indicate the generation of read methods for symbolic parameters.
Attr_writer is generally followed by a symbolic parameter and defines one or more write-only attributes to indicate the write Method for symbolic parameters.
Attr_accessor is generally followed by a symbolic parameter, which defines one or more read/write attributes and is used to indicate that a symbolic parameter is used to generate a read/write method.


Why use symbols?
We may be wondering, why should we use a symbol (colon: + variable) after attr )?


In fact, we do not need symbols.
class Person  attr "name",trueendp = Person.new()p.name = "xiao ming"puts p.name




We need to know that symbols are unchangeable strings and cannot be GC.
In most cases, the method that accepts symbols as parameters can also accept strings, which in turn cannot be true;
Each symbol appears only once in the object space, saving memory with symbols


Why does Ruby runtime ensure that every symbol is unique? This is because Ruby stores symbol in a symbol table maintained during runtime, and this symbol table is actually an atom data structure, which stores all the current program-level names, make sure that multiple objects with the same content are not displayed. Almost every language and system has such a symbol table, except for a language like C/C ++. This symbol table only exists during compilation and does not exist during runtime. While Python and Ruby keep this table for backup at runtime.


Why do we only use symbols without strings? I think the two main aspects are memory and efficiency.
Two strings with the same content in Ruby are actually two different objects.
A = "hello"
B = "hello"
Although both strings have the same content, you know a. object_id when comparing a and B! = B. object_id, which points to not the same object.
If a string is used, p. name = "xiaoming" and p. name = "Xiaohong" may occupy 2 times of memory.
In terms of efficiency, it may be to avoid multiple dynamic generation of strings.

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.