Ruby Diary 1

Source: Internet
Author: User



1.IRB parameter Configuration



~/.irbrc


IRB.conf[:PROMPT_MODE] = :SIMPLE #Simplify the irb prompt and disable some annoying auto-shrinking
IRB.conf[:AUTO_INDENT_MODE] = false


2. Notes:



#Single line



=begin



...... MultiRow



=end



3. String



String connection


>> "JK" + "JK" = "JKJK"


string interpolation


>> one = "foo" + "foo" >> "#{one} is one" and "Foo is one"


String printing


>> puts "jk" # comes with a newline after printing
Jk
=> nil
>> p "jk" #Print out the same as the original
"jk"
=> "jk"
>> print "jk" #Print does not wrap
Jk=> nil


Single double quote string


>> ‘jk\n#{one}‘ #Do not transfer characters and interpolate operations
=> "jk\\n\#{one}"
>> "jk\n#{one}" #output the escaped characters and interpolation operations
=> "jk\nfoo"


Object and Message Delivery


>> "jk".length
=> 2
>> "".empty?
=> true
>> "jk".nil?
=> false
>> "jk".include?("j")
=> true


When judging, you can cooperate with && (and) | | (OR)! (not) to operate


>> one = "foo" 
=> "foo"
>> two = ""
=> ""
>> if one.empty? && two.empty?
>> print "both empty"
>> elsif one.empty? || two.empty?
>> print "empty exist"
>> elsif !x.empty?
>> print "x is not empty"
>> end
empty exist=> nil


To_s can convert any object into a string


>> nil.to_s.empty?=> True


Unless judgment


>> two = "dasd"
=> "dasd"
>> print "ss" unless two.empty?
ss=> nil


Use!! Can be reversed twice to convert the object to a Boolean type


>> !!nil #nil object is boolean
=> false
>> !!0 #Others are true
=> true


Values and Ranges



Split method to split a string into a desired character array


>> "dasjkd das d dasd".split #default to remove spaces
=> ["dasjkd", "das", "d", "dasd"]
>> "fdjaskl,jfdskl,jfklasd".split(",") #Setup, split
=> ["fdjaskl", "jfdskl", "jfklasd"]


Index of the array


>> a=[12,34,45,67,89,8]=> [8]>> a[0]=> 12>> a[2]=> 45>> a[-4] #索引可. is negative = 45
>> a.fifth #Can look like this, but this is the effect rails adds
= 89
>> A.first
= 12
>> A.last
= 8

>> a=[12,34,45,67,89,08] #Note numbers cannot have 0 before, warning error Invalid decimal
SyntaxError: (IRB): 40: invalid octal digit
A=[12,34,45,67,89,08]
^


A series of methods for arrays





>> a.length
=> 6
>> a.include?(45)
=> true
>> a.sort # Arrange in ascending order (do not change the original array)
=> [8, 12, 34, 45, 67, 89]
>> a.reverse #order in turn
=> [8, 89, 67, 45, 34, 12]
>> a.shuffle #Random chaos => [45, 12, 8, 34, 67, 89]
>> a.sort! #Arrange in ascending order (change original array)
=> [8, 12, 34, 45, 67, 89]
>> a.push(4) #Add element (change original array)
=> [8, 12, 34, 45, 67, 89, 4]
>> a << 3 #Add elements (change the original array)
=> [8, 12, 34, 45, 67, 89, 4, 3]
>> a << "tu" << "re" #Add in series, and other types of elements can be added to the array
=> [8, 12, 34, 45, 67, 89, 4, 3, "tu", "re"]
>> a.join #merge strings
=> "8123445678943ture"
>> a.join(",") #add connector
=> "8,12,34,45,67,89,4,3,tu,re"


Range operations


>> (0...9). To_a # remove the last value, converting to an array
= = [0, 1, 2, 3, 4, 5, 6, 7, 8]
>> (0..9). To_a # Keep all to
= = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>> a= (0..9). to_a
= = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> a[4: ( A.length)] #using the length method
= = [4, 5, 6, 7, 8, 9]
>> A[4..-2] #小技巧
= = [4, 5, 6, 7, 8]


Block


>> (1..9). Do |f|? > Print f*f>> end149162536496481
>> (1..9). Map {|i| I*i} #transform each element and return a map
= = [1, 4, 9, 16, 25, 36, 49, 64, 81]
>> (1..9). reduce {|i| I*i} #convert the entire array to a value return
= 1
>>%w{das das das} #%w transform element to string return
= = ["Das", "Das", "Das"]


Hashes and symbols

>> user = {} #initialize hash
== {}
>> user["one"] = "yz00"
= "Yz00"
>> user["both"] = "yz01"
= "yz01" >> user["one"] #Get hash
= "Yz00"
>> User
+ = {"One" = "yz00", "One" and "yz01"}>> user = {"One" = "yz00", "one" = "yz01"} # can also be assigned this way
+ = {"One" = "yz00", "One" and "yz01"} #Note that there is a space after the comma, the conventional


You can think of a symbol as a string without constraints, so it can be written in the following form


>> user = {: one = "yz00",: "yz01"} #Do not initialize == {:one=> "yz00",:two=> "yz01"}


Since the symbol is used more as a key, it is defined after 1.9


>> user = {one: "yz00", "One", "yz01"}
= = {:one=> "yz00",:two=> "yz01"}


Nested hashes


>> Users[:user] = user
= = {:one=> "yz00",:two=> "yz01"}
>> Users
= = {:user=>{:one=> "yz00",:two=> "Yz01"}}
>> Users[:user][:one]
= "Yz00"
>> print (1..5). to_a
[1, 2, 3, 4, 5]=> Nil
>> puts (1..5). to_a
1
2
3
4
5
= Nil
>> puts (1..5). To_a.inspect #inspect method outputs the literal form of the array, the output object is equivalent to the P method
[1, 2, 3, 4, 5]
= Nil
>> p (1..5). to_a
[1, 2, 3, 4, 5]
= = [1, 2, 3, 4, 5]





Ruby Diary 1


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.