Array and hash table
Ruby arrays and hash tables are index sets. Both objects are saved and can be read by keys. The key of the array is a number, but the hash table supports the object as the key. They all grow with the addition of new elements. In terms of access elements, the array efficiency is relatively high, but the hash table is more flexible. Different types of objects can be stored in both arrays and hash tables. You can use an array to save numbers, strings, and floating-point numbers, as you will see.
You can use array text to create and initialize a new array-a set of elements surrounded by brackets. For an array, you can use numbers between brackets to obtain each element, as shown in the example. Note that the ruby array starts from 0.
A = [1, 'ccat', 3.14] # array with three elements
# Access the first element
A [0]-> 1
# Set the third element
A [2] = Nil
# Dump out the Array
A-> [1, "cat", nil]
You may have noticed that we use a special value nil in the example. In many languages, the concept of nil (or null) is "no object ". In Ruby, this is not the case; NIL is an object, just like other objects; it represents nothing. In any case, let's go back to the array and hash table.
Sometimes it is painful to create a Word Array. It contains many quotation marks and commas. Fortunately, Ruby has a shortcut: % W can be used only for what we want to do.
A = ['ant', 'bee ', 'cat', 'dog', 'elk']
A [0]-> "ant"
A [3]-> "dog"
# This is the same:
A = % W {ant bee cat dog elk}
A [0]-> "ant"
A [3]-> "dog"
Ruby's hash table is similar to an array. The hash table uses curly brackets instead of braces. A hash table provides two objects for each item: one is a key and the other is its value.
For example, you want to represent music equipment for a band. You can use a hash table to implement it.
Inst_section = {
'Cello '=> 'string ',
'Clarinet '=> 'woodwind ',
& Apos; Drum & apos; = & apos; percussion & apos ',
'Oboe' => 'woodwind ',
'Trumpet '=> 'brass ',
'Violin' => 'string'
}
On the left is the key and on the right is its corresponding value. Keys are unique in each specific hash table. You cannot have two "drum" items. Keys and values can be arbitrary objects in a hash table-you can hash an array, or even other hash tables.
Hash Tables use the same brackets as arrays.
Inst_section ['oboe']-> "woodwind"
Inst_section ['cello ']-> "string"
Inst_section ['bassoon']-> Nil
As shown in the last example, if the key does not exist, the hash table returns nil by default. In general, this is very convenient. In conditional expressions, nil indicates false. Sometimes you want to change the default value. For example, if you want to calculate the number of access times in the hash table, it is more convenient if the default value is 0. When creating a new empty hash table, you can easily specify a default value.
Histogram = hash. New (0)
Histogram ['key1']-> 0
Histogram ['key1'] = histogram ['key1'] + 1
Histogram ['key1']-> 1
Control Structure
Ruby has all common control structures, such as if statements and while loops. Java, C, and PerlProgramAn error occurs because curly braces are missing in the statement body. Instead, Ruby uses the keyword end to indicate the end of the statement body.
If count> 10
Puts "try again"
Elsif tries = 3
Puts "you lose"
Else
Puts "enter a number"
End
Similarly, the while statement ends with end.
While weight <100 and num_pallets <= 30
Pallet = next_pallet ()
Weight + = pallet. Weight
Num_pallets + = 1
End
Most declarations in Ruby have a return value. You can use it as a conditional statement. For example, the gets method returns the next row from the standard input stream or nil when the end of the file is reached. Because Ruby regards nil as false in the Condition Statement, you can use the followingCodeTo process all lines of the file.
While line = gets
Puts line. downcase
End
Here, this Declaration sets the value of the variable line to the next line or nil, and then while detects this variable, ends the loop when it is nil.
When the if or while statement has only one line of code, Ruby's code modifier is useful and simplified. You only need to write a simple conditional statement after the expression. For example, the following is a simple if code example.
If radiation> 3000
Puts "Danger, Will Robinson"
End
Again, this code is rewritten with a code modifier.
Puts "Danger, Will Robinson" If radiation> 3000
The code similar to the while loop is as follows:
Square = 2
While side <1000
Square = square * Square
End
More concise
Square = 2
Square = square * Square while square <1000
This code modifier looks familiar to Perl programs.