Ruby Basic types, dynamic features, code blocks

Source: Internet
Author: User


#Ruby Built-in basic data type Nilclass,tureclass,falseclass,time,date,string,range,struct,array,hash#Numerice 1. Divided into Interger integer class. Interger is divided into Fixnum and bignum. #2. #Float浮点数#3.Complex plural. #dynamic characteristics of Bury#1. Dynamically execute code eval in string form#2. Dynamically obtain the values of constants and variables in a module or class#3. Dynamically add a method to a class or object#4. Dynamic processing of known variables and methods#5. Dynamic Delete definition#Eval compiles and executes the string. The eval of JavaScript=Beginclass_eval The string as a code compilation within the context of the class Module_eval executes the string as code in the context of the class or module Instance_eval is compiled in the context of the instance =EndclassString PI="FJSKDJF" defdosome (n) self[0,n]+ (Self.size > n?)"..":"")#spaces cannot be ignored =_=| |End forIinch[5,6,7,8] Module_eval"def Dosome_#{i}Dosome (#{i})End"endendputs"SDFSDF". Dosome_5puts"SDFDSFSDF". Dosome_6puts"GSFSDF". UpCase puts"SDFSDF". Send ("upcase")#method of the Send method to invoke the object dynamically#puts "SADFD". Methods#puts String.methods #获取对象的所有方法名#Instance_methods Returns all exposed instance method names, Protected_methods returns the protected method namePuts string.method_defined? (: UpCase)#checks whether an instance method is defined by an objectPuts"String". respond_to? (: UpCase)#checks whether an object can be matched to a call to an instance methodPuts String.const_get ("PI")#gets the value of a constant in a module's class based on the name of the constantputs String::P i#The const_missing,method_missing method allows you to customize the returned error message when an object calls a method or constant that does not exist. Array=[1,3,4,5,22,33]array<<[1,44,5,6,7]#adding elementsArray.push ([333,1234,1233333])#adding elementsArray.each {|item|puts item}
Lambda and Proc,block
defTest (arg,arg2,&block)#parameters passed to the code block after processing the parameters of the method  yieldARG+ARG2,"the second parameter of a code block"#takes up two bits, so the following call will output 2 times, and the code block is actually a proc object  yieldARG+ARG2,"a second parameter"#The yield here is actually quite similar to the following functionBlock.call (ARG+ARG2,"The second argument of the proc object") End#Test (one) {puts "yield placeholder"}#Test do#puts "Today is a good day."#End#two ways to create code blocks {code}, do code endTest"what thing?","Entebbe") {|x,y| puts x+Y}block=proc.new{|o,x| puts o+x+"proc Method"}test ("What, what?","Regular Army",&block)#the parameters of the code block are passed with the | parameter | Yield (x, y) pass parameters to code blocks#the advantage of using the Proc method is that you can feel the code block argument passed to the method, and the Proc object does the method pass with the & symboldefF0 () p= proc.new {return0} p.call1EnddefF1 () L=Lambda{return0} l.call1endf0#returns 0F1#returns 1#If you can understand that proc is more like block,lambda in behavior than it is simply anonymous, then you won't be surprised by the results above. #If you make some changes to the F0,F1, it will be easier to understand the results above. defF0 ()return01EnddefF1 ()def __f1    return0 End__f11endf0#returns 0F1#returns 1
Exception handling
def extest (x, y)  begin#try  z=x/y  =>err#catch      puts err     #retry redo  ensure#finaly  " operation complete "  end  endextest (1,0)

Ruby Basic types, dynamic features, code blocks

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.