Ruby journey (ii) basic syntax of Ruby, object usage, method definition and calling, attribute definition and usage

Source: Internet
Author: User

(1) 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. You can also use the-37 fixnum instance method ABS to obtain the absolute value:-37.abs() will return 37
If you enter 1.1.class, float is returned.

(2) Ruby syntax
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 declared using attr_writer and attr_reader, which correspond to the set and get of C # respectively. attr_accessor is readable and writable.
The $ identifier of the global variable in ruby.
Constants (constants) in Ruby start with uppercase letters, and the conventions are all uppercase letters.

Any expression in Ruby will return the value, sample

Class Rectangle
Def Initialize (wdth, HgT)
@ Width = Wdth
@ Height = HgT
End
Def Width = (Wdth)
@ Width = Wdth
End

End

R = Rectangle. New ( 2 , 3 )
Puts R. Width =   5   # Output 5
Puts R. Width # Error! Because the width not support read

Continue to supplement the use of attr_accessor, sample


Class Rectangle
Attr_accessor: width
Attr_accessor: Height
Attr_accessor: wid22.
Attr_accessor: height2

Def Initialize (wdth, HgT)
@ Width = Wdth
@ Height = HgT
End

Def Area ()
Return @ Width * @ Height
End

Def Area2 ()
Return @ Wid22. * @ Height2
End


End

R = Rectangle. New ( 2 , 3 )
R. Width =   5   # Give samename's variable value
R. Height =   5  
Puts R. Area () # Outputs is 25

R. wid22. =   6   # Not samename's variable create
R. height2 =   6  
Puts R. area2 () # Outputs is 36

The aboveCodeIt indicates whether a member variable with the same name exists when you use attr_accessor. If yes, access the member variable with the same name. If no member variable with the same name exists, a leading @ member variable is created by default.

(3) Amazing Operator Overloading
Ruby supports Operator overloading, which is amazing!


Class Rectangle
Attr_accessor: width
Attr_accessor: Height

Def Initialize (wdth, HgT)
@ Width = Wdth
@ Height = HgT
End

Def Area ()
Return @ Width * @ Height
End

Def   + (Addrectangle)
Return Self. Area + Addrectangle. Area
End

End

R1 = Rectangle. New ( 2 , 2 )
R2 = Rectangle. New ( 3 , 3 )
Puts r1 + R2 # Operator override
Puts r1 + (R2)
Puts R1. + (R2) # Standard function calling format

It's amazing. Actually, it's best to think of the "+" as a function name, just like the last one, haha.

(4) passing Parameters
The default value and variable length parameters are characteristic of parameter passing. Other languages also have Ruby.

1. Default Value of the Parameter
The default value is very simple. Like other languages, sample

Class Rectangle
Attr_accessor: width
Attr_accessor: Height

Def Initialize (wdth =   2 , HgT =   2 )
@ Width = Wdth
@ Height = HgT
End

Def Area ()
Return @ Width * @ Height
End

End

R1 = Rectangle. New
Puts r1.area

Now, use the default value.

2. optional parameter, variable length parameter sample

Class Paramsample
Def Sayhello ( * Names)
Puts names. Class
Puts " Hello # {names. Join ( " , " )}! "
End

End

PS = Paramsample. New
PS. sayhello # Output array hello!
PS. sayhello ( " Lee " , " Snake " ) # Output array Hello Lee, snake!

We can see that the variable length parameter prefix * is actually an array.

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.