Ruby Learning Note 0505

Source: Internet
Author: User

1) related knowledge of classes, objects, methods, variables, etc.

classfixnum #Ruby下的数值的类型是Fixnum或者Bignum @ @words= ["Zero"," One"," Both","three"]; #类方法 alias Original_addition+ #使用alias重命名关键词defsay #向Fixnum中添加新的对象方法 P @ @words [self.to_i]; #self表示对象自身 Enddef+(value) #重定义 + method Original_addition (value). SUCC #succ表示下一个数字 Endclass<<Self #Ruby下定义类方法的常用方法, in which test and test1 are class methodsdefTest EnddefTest1 End EndEnd

In Ruby.

Methods: Class method, Object method, single state method, etc.

Variables involved in the class: Global variable ($ start), class variable (beginning with @@), instance variable (@ start), constant (all letters capitalized)

Object: Object is new (the new method is defined in object, other classes inherit from it), and the base object such as 1, 232, "asdf", etc.

2) code block related knowledge

You can use Do...end or {...} for calls with code block methods. Try to use the do ... end when iterating, and use the {...} for the resource management of the code block, but with the same effect, the {} has precedence over Do...end

A) Do...end

1 array = [All-in-all,"a",: a,:b];            2 array.each do |i|    #使用do: End to specify a code block that loops within the array of 3     if  i.is_a? ( Numeric)4

Array.each_with_index do |item,i| #数组的each_with_index方法包括元素和下标的值
p [Item,i];
End

b) {...}

1 a=[1,34,21,2,30,563]2 p a.sort{|x,y|  3                When X.to_s.length>y.to_s.length then  14when                x.to_s.length<y.to_s.length  then-15                 Else  06           End}

c) Use yield to replace the code block, that is, the need to call the code block when the yield

(a) yield with parameters

1 def One_block 2      for  in 1..33     yield(num) #调用的就是do ... end code, NUM passed in 4 as the |i| parameter in the code snippet      End5end6 one_block do |i| 7     "  "  #   #{exp} inserts the value into the string and supports all escape characters, where the value is converted to a string.  8     End

(b) How yield matches when there are multiple blocks of code

1 defdo_something2     yield3 End4 5 do_something Do6(1..9). Each {|i|PrintIifI<5}7 puts8 End9 do_something DoTen3.times {Print "hi!" } One puts AEnd

Output
1234
hi! Hi! hi!

3) Usage of% in Ruby

%Q (...) represents a double-quoted string

%q (...) represents a single quote string

%w (...) represents an array where the elements are enclosed in double quotation marks.    eg. = =%W (#{foo} Bar bar\ with\ space) #=> ["foo", "Bar", "Bar with Space"]

And so on, see:

Http://www.jb51.net/article/49868.htm

Ruby Learning Note 0505

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.