Ruby Usage Summary (EMC)

Source: Internet
Author: User

1. The conversion between integers, floating-point numbers, and strings in Ruby

The classes of ruby integers, floating-point numbers, and strings provide to_i,to_f,to_s three methods for converting integers, converting them to floating-point numbers, and converting them into strings.

2. Array traversal

Example 1:
a=['a','b','C']a.each {|x|puts X}
Example 2:ps=["EMSD","XIO","DPAD","ISD","DSSD","ASD","VCE","RSA","IIG","N /A"]ps.each{|x| Innovationproduct.create (:bu=>x,:innovation_product_id=>0)}
Example 3:products=Innovationproduct.get_products_by_bu (BU) productsarray=array.new forJinchProducts Productsarray.push (j.product) End

3. If Judgment statement

Example 1:

A=1if a==1    1  #打印1elsif a==2    2else    3 End

Example 2:

  A=false  if  a      1  elsif!a      2  # Print 2  Else   3 end      

4, puts and print difference: Puts line, print does not wrap.

5. How to get input from the command line in Ruby: Gets, the input variable is stored in $_

" Please enter the temperature: " gets     #输入 "AAABBBCCC" puts $_  #输出 "AAABBBCCC"

6. Symbolic symbol (:)

In Ruby, Symbol denotes "name", such as the name of the string, the name of the identifier. The way to create a Symbol object is to precede the name or string with a colon:

Create a Symbol object, for example:

: Foo:test

: "ABC" #结果是: "I am a Boy"

String is a string, why do you have the name of the string? This is because a string is also an object in Ruby, a string object. Both the structure and the operation and the Symbol object are different.

7, in Ruby each object has a unique object identifier (Identifier), you can use the Object_ID method to get an identifier for an object.

Example: puts:foo.object_id

Ruby Usage Summary (EMC)

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.