I. String operations
String Interpolation
1. # {}: Any valid Ruby Code placed in # {} will be evaluated and replaced with the evaluated result and inserted to the original location.
2. Differences between single quotes and double quotes:
Double quotation marks allow character escaping. single quotation marks do not allow escape. What are the characters in the quotation marks and what are the characters you can see?
Single quotes do not allow character Interpolation
String splitting
1. split by space: string. split ('') string connection
Method 1: 'Ruby '+ 'monk', stdout: RubyMonk
Method 2: "Monk". concat ("Ruby") stdout: MonkRuby
Method 3: "Ruby" <"Monk" stdout: RubyMonk
Substring replacement
Replace the first search Target: "I shoshould look into your problem when I get time". sub ('I', 'we ')
Replace all qualified strings: "I shoshould look into your problem when I get time". gsub ('I', 'we ')
Ii. Condition and loop: control structure in Ruby
Ruby Boolean expression
Ruby uses the = Operator to compare objects: name = "Bob"
Non-expression :! (Name = 'bob ')
If... else is structured in Ruby ,? And: it can be understood that they correspond to loops in the "then" and "else" Ruby languages respectively.
1. Infinite Loop
Copy codeThe Code is as follows:
Loopdo
.....
End
2. execute code snippets N times
Copy codeThe Code is as follows: times do
......
End
3. Array
Create an empty array'[]' Or Array. new
Query data in an array
Copy codeThe Code is as follows: [1, 2, 3, 4, 5] [2]
The result is
Copy codeThe Code is as follows: 3
Ruby indexes start from 0 and start from-1.
Array Growth
Copy codeThe Code is as follows:
[1, 2, 3, 4, 5] <"woot" results
[1, 2, 3, 4, 5, "woot"]
Basic Array Operations
1. array transformation
Copy codeThe Code is as follows: [1, 2, 3, 4, 5]. map {| I + 1}
Result
Copy codeThe Code is as follows: [2, 3, 4, 5, 6]
2. Filter Array elements
Copy codeThe Code is as follows: [1, 2, 3, 4, 5, 6, 7]. delete_if {| I <4}
Result
Copy codeThe Code is as follows: [2, 4, 6]
Extract strings longer than five letters
Copy codeThe Code is as follows: names = ['Rock ', 'paper', 'scissors', 'lizard ', 'spock']
Names. select {| word. length> 5}
Result
Copy codeThe Code is as follows: ["scissors", "lizard"]
3. delete an element
Delete the element '5' of the following Array'
Copy codeThe Code is as follows: [1, 3, 5, 4, 6, 7]. delete 5
[1, 2, 3, 4, 5, 6, 7]. delete_if {| I <4}
Iteration 1. each loop
Copy codeThe Code is as follows: array = [1, 2, 3, 4, 5]
Array. each do | I |
Puts I
End
Iv. Ruby hashing
1. Create a hashEmpty Hash can be defined using two braces {}. Hash. new
Copy codeThe Code is as follows:
Export ant_menu = {
"Ramen" => 3,
"Dal Makhani" => 4,
"Tea" => 2
}
2. Take values from the hashGet the price of a bowl of Ramen (Ramen) from the restaurant_menu hash
Restaurant_menu ["Ramen"] Result
3. Modify the hash to add a new item
Copy codeThe Code is as follows:
Export ant_menu = {}
Export ant_menu ["Dal Makhani"] = 4.5
Ant_menu ["Tea"] = 2 4. traverse the hash
Export ant_menu = {"Ramen" => 3, "Dal Makhani" => 4, "Coffee" => 2}
Restaurant_menu.each do | item, price |
Puts "# {item }:$ # {price }"
End
Result
Copy codeThe Code is as follows:
Ramen: $3
Dal Makhani: $4
Coffee: $2
Use the each method to increase the price of all items in restaurant_menu by 10%.
Copy codeThe Code is as follows: Export ant_menu = {"Ramen" => 3, "Dal Makhani" => 4, "Coffee" => 2}
Restaurant_menu.each do | item, price |
Export ant_menu [item] = price + (price * 0.1)
End
5. Get the key and value from the hash
Each hash object has two methods: keys and values.
The keys method returns an array containing all the keys in the hash. Similarly, the values method returns an array containing all the values.
Obtain all keys in the restaurant_menu hash.
Copy codeThe Code is as follows: Export ant_menu = {"Ramen" => 3, "Dal Makhani" => 4, "Coffee" => 2}
Restaurant_menu.keys
Result
Copy codeThe Code is as follows:
["Ramen", "Dal Makhani", "Coffee"]
V. Category
Ruby uses two spaces for indentation. A code block is usually closed with the keyword end. Generally, an instance is created when a new method is called on a class.
Copy codeThe Code is as follows: Object. new
1. Build your own class
Copy codeThe Code is as follows: class Rectangle
End
Calculates the circumference and area of a rectangle.
Copy codeThe Code is as follows: class Rectangle
Def initialize (length, breadth)
@ Length = length
@ Breadth = breadth
End
Def perimeter
2 * (@ length + @ breadth)
End
Def area
@ Length * @ breadth
End
End