Programming. Ruby Study Notes (Chapter 2 and Chapter 3)

Source: Internet
Author: User

Ruby processes single quotes and double quotes differently.

(1) double quotation marks can be applied to escape characters

(2) strings in double quotation marks can be replaced by # {varname}. For global variables or instance variables, the {} symbol is not required. ------ I don't quite understand the differences between the two variables

 

Nil is also an object in Ruby, but it means nothing.

 

The statement of Ruby's control structure does not have a scratch sign. End indicates the end.

The numeric type in ruby is immutable, that is, the passed value, not the reference.

 

Chapter 3: definitions of Objects

In the definition of the Ruby class, the constructor is the initialize function, a new object is initialized to XX. New, And the tostring method of Ruby is to_s.

Adding a new method to Ruby is too high, as long as it is directly added, it is more flexible than Js.

Ruby inheritance uses the <symbol. Super calls the method of the same name of the parent class.

Ruby's @ instance variable is equivalent to a private field and cannot be accessed externally. to access it, you must define a method similar to the attribute to return a result. Attr_reader: name,: age,: sex, attr_reader

Is a keyword to simplify this operation. This is actually a method, which is a manifestation of Ruby metaprogramming and will dynamically generate the passed parameters.

 

Def name = (name)

@ Name = Name

End

This is a way to define writable attributes. Remember to end with =.

Class variables are declared as @, which is equivalent to static methods and private. To be accessed externally, you must open the classname. var.

 

Import external files using the load 'xxx. rb' method.

It should be said that too many keywords are used to write a singleton mode...

There are also three access control levels, but the syntax is different. The method of putting them in the final declaration is more elegant.

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.