- 浏览: 25627 次
- 性别:
文章分类
最新评论
Hashes are powerful collections. They have many methods that programmers can use to do their work.
Hash creation
A hash can be created in two basic ways. Either with the new keyword or by utilizing the hash literal.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
puts names
The first script creates a hash and adds two key-value pairs into the hash object.
names = Hash.new
A hash object is created.
names[1] = "Jane"
names[2] = "Thomas"
We add two pairs of values to the hash. The 1, 2 numbers are the keys to the hash. The keys are placed inside the square brackets. The names are the values that belong to the keys.
puts names
The puts method prints the string representation of the hash to the console. It is also the string literal of the hash.
$ ./create.rb
{1=>"Jane", 2=>"Thomas"}
From the output we can see the literal representation of the names hash. A hash is bouded by curly brackets. The keys and the values are paired with the => characters.
A store method can be used to initialize the hash with some values. It can be use instead of the square brackets.
#!/usr/bin/ruby
names = Hash.new
names.store(1, "Jane")
names.store(2, "Thomas")
names.store(3, "Rebecca")
puts names
We have a similar script. This time we use the store method. The method associates the given key with the given value and stores the pair in the hash.
names.store(1, "Jane")
The first parameter of the store method is the key and the second parameter is the value.
In the third script, we create a hash with the hash literal notation. The values are bound by the curly brackets. And the key-value pairs are associated with the => characters.
#!/usr/bin/ruby
domains = { "de" => "Germany",
"sk" => "Slovakia",
"hu" => "Hungary",
"us" => "United States",
"no" => "Norway"
}
puts domains["de"]
puts domains["sk"]
We create a domains hash with 5 pairs. This time both keys and values are string types.
domains = { "de" => "Germany",
"sk" => "Slovakia",
"hu" => "Hungary",
"us" => "United States",
"no" => "Norway"
}
This is a hash literal notation. The key-value pairs are put between the curly brackets. The items are separated by the comma character. And the keys are associated with values using the => characters combination.
puts domains["de"]
Here we print the domain value name associated with the "de" key.
$ ./create3.rb
Germany
Slovakia
Output of the code example.
Basic work
In this section, we will present some methods for the very basic work with Ruby hashes.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
names[3] = "Robert"
names[4] = "Julia"
names[5] = "Rebecca"
puts "The size of the hash is #{names.size}"
puts names.keys.inspect
puts names.values.inspect
In the above Ruby script, we create a hash with five values. We introduce three hash methods.
puts "The size of the hash is #{names.size}"
The size method returns the size of the hash. It is a synonym for the length method.
puts names.keys.inspect
puts names.values.inspect
The keys method returns all keys of the hash. In a similar fashion, the values method returns all the values of the hash. The returned data is in the form of an array. To have a more readable output, we also call the inspect method on the returned arrays.
$ ./basic.rb
The size of the hash is 5
[1, 2, 3, 4, 5]
["Jane", "Thomas", "Robert", "Julia", "Rebecca"]
Ouput of the example. Note that the output of the last two methods are two arrays.
The second example of the section presents three distinct hash methods.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names1[3] = "Robert"
names1[4] = "Julia"
names1[5] = "Rebecca"
names2 = names1.dup
puts names1.eql? names2
puts names1.empty?
names1.clear
puts names1.empty?
The Ruby script creates a names hash. It calls three hash methods on the object.
names2 = names1.dup
We create a duplicate of the hash by calling the dup method. The method is inherited by the hash from the parent object.
puts names1.eql? names2
The eql? method compares two hash objects. In our case the hashes are equal and the line prints true.
puts names1.empty?
The empty? method checks whether the hash is empty or not. The line print false. Because the names1 hash has five items.
names1.clear
puts names1.empty?
The clear method deletes all items from the hash. The successive call of the empty? method returns true.
$ ./basic2.rb
true
false
true
Example output.
We have methods that can determine, whether a key or a value is present in the hash.
#!/usr/bin/ruby
domains = { :de => "Germany", :sk => "Slovakia",
:no => "Norway", :us => "United States"
}
puts domains.has_key? :de
puts domains.include? :no
puts domains.key? :me
puts domains.member? :sk
puts domains.has_value? "Slovakia"
puts domains.value? "Germany"
We create a domains hash with four pairs. The keys are symbols. Symbols are often used as keys, because they are more efficient.
puts domains.has_key? :de
puts domains.include? :no
puts domains.key? :me
puts domains.member? :sk
Here we have four methods that determine, whether a key is in the hash. They all do the same; they are synonyms.
puts domains.has_value? "Slovakia"
puts domains.value? "Germany"
These two methods check if the two strings are inside the hash.
$ ./has.rb
true
true
false
true
true
true
This is the output of the example.
In the final example of the section, we will read values from the hash.
#!/usr/bin/ruby
stones = { 1 => "garnet", 2 => "topaz",
3 => "opal", 4 => "amethyst"
}
puts stones.fetch 1
puts stones[2]
puts stones.values_at 1, 2, 3
The Ruby script presents three hash methods for reading values of a hash.
puts stones.fetch 1
The fetch method reads a value for a given key.
puts stones[2]
Square brackets can be used to get a value. In our case, the line prints "topaz" to the console.
puts stones.values_at 1, 2, 3
The values_at method can be used to get multiple values at one step. The method returns an array of the values for the given keys.
$ ./read.rb
garnet
topaz
garnet
topaz
opal
Output of the example.
Looping through a hash
There are several methods that can be used to loop through a Ruby hash.
#!/usr/bin/ruby
stones = { 1 => "garnet", 2 => "topaz",
3 => "opal", 4 => "amethyst"
}
stones.each { |k, v| puts "Key: #{k}, Value: #{v}" }
stones.each_key { |key| puts "#{key}" }
stones.each_value { |val| puts "#{val}" }
stones.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
In the above example, we present four methods. We use them to display all keys, values and both keys and values of a hash.
stones.each { |k, v| puts "Key: #{k}, Value: #{v}" }
The each method calls the given block for each key in the hash, passing key-value pair as parameter.
stones.each_key { |key| puts "#{key}" }
We use the each_key method to loop throug all keys of a hash. They are printed to the console.
stones.each_value { |val| puts "#{val}" }
The each_value can be used to loop throug the values of a hash.
stones.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
The each_pair method is a synonym for the each method. We loop through the keys and values of the stones hash.
$ ./loop.rb
Key: 1, Value: garnet
Key: 2, Value: topaz
Key: 3, Value: opal
Key: 4, Value: amethyst
1
2
3
4
garnet
topaz
opal
amethyst
Key: 1, Value: garnet
Key: 2, Value: topaz
Key: 3, Value: opal
Key: 4, Value: amethyst
The output shows the keys and values, keys, values of the stones hash.
Deleting pairs
In the following examples, we will concern ourselves with methods that delete pairs from the hashes. There are methods that delete only a pair at a time and methods, that can delete multiple key-values at one step.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
names[3] = "Robert"
names[4] = "Julia"
names[5] = "Rebecca"
names.delete 4
names.shift
puts names
In the script we have two methods. The delete method and the shift method. The delete method removes and returns a value for a specified key. The shift method deletes the first pair from the hash. It also returns the removed pair as an array.
names.delete 4
Here we delete a pair 4 => "Julia".
names.shift
This code line removes the first pair, namely 1 => "Jane".
$ ./deleteitem.rb
{2=>"Thomas", 3=>"Robert", 5=>"Rebecca"}
In the output we can see the pairs of the hash that are left.
The reject and the delete_if methods can remove multiple pairs from a hash. The methods delete pairs that return true for the given condition in the block. There is an important distinction between the two methods. The reject method works on a copy of a hash while the delete_if works on the original hash.
#!/usr/local/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names1[3] = "Robert"
names1[4] = "Julia"
names1[5] = "Rebecca"
puts names1.reject { |k, v| v =~ /R.*/ }
puts names1
puts names1.delete_if { |k, v| k<=3 }
puts names1
The example deletes multiple pairs using the previously mentioned methods.
puts names1.reject { |k, v| v =~ /R.*/ }
The reject method removes all values that fit the regular expression in the block. The modified hash is returned and the original hash is not changed.
puts names1
The output of this line confirms, that the original hash was intact.
puts names1.delete_if { |k, v| k<=3 }
In this case, we delete all pairs, for which the key is lower or equal to 3. The method modifies the original hash.
$ ./massdelete.rb
{1=>"Jane", 2=>"Thomas", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia", 5=>"Rebecca"}
{4=>"Julia", 5=>"Rebecca"}
{4=>"Julia", 5=>"Rebecca"}
Output of the example.
Adding hashes
Ruby has methods for hash addition. The merge and the update methods.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names2 = Hash.new
names2[3] = "Robert"
names2[4] = "Julia"
names = names1.merge names2
puts names
names = names1.update names2
puts names
In the Ruby script, we create two hashes. Then we apply merge and update methods on them.
names = names1.merge names2
puts names
We add two hashes, the names1 and the names2 and the result is assigned to the names hash. We print the newly created hash.
$ ./merge.rb
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
As we can see, the final hashes contain pairs from the names1 and names2 hashes.
The merge vs merge!
In the final section, we recap a common ruby thing. Several methods have their counterparts, that end with an explamation mark. The mark has no syntactical meaning. It is a Ruby idiom. It hints a programmer, that the method will actually modify an object, on which the method is called.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names2 = Hash.new
names2[3] = "Robert"
names2[4] = "Julia"
names = names1.merge names2
puts names
puts names1
names = names1.merge! names2
puts names
puts names1
We will demonstrate the difference on the merge and merge! methods.
names = names1.merge names2
The merge does not modify the names1 hash. It works on its copy.
names = names1.merge! names2
The merge! method works on the original hash. The names1 hash is changed.
$ ./merge2.rb
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
Hash creation
A hash can be created in two basic ways. Either with the new keyword or by utilizing the hash literal.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
puts names
The first script creates a hash and adds two key-value pairs into the hash object.
names = Hash.new
A hash object is created.
names[1] = "Jane"
names[2] = "Thomas"
We add two pairs of values to the hash. The 1, 2 numbers are the keys to the hash. The keys are placed inside the square brackets. The names are the values that belong to the keys.
puts names
The puts method prints the string representation of the hash to the console. It is also the string literal of the hash.
$ ./create.rb
{1=>"Jane", 2=>"Thomas"}
From the output we can see the literal representation of the names hash. A hash is bouded by curly brackets. The keys and the values are paired with the => characters.
A store method can be used to initialize the hash with some values. It can be use instead of the square brackets.
#!/usr/bin/ruby
names = Hash.new
names.store(1, "Jane")
names.store(2, "Thomas")
names.store(3, "Rebecca")
puts names
We have a similar script. This time we use the store method. The method associates the given key with the given value and stores the pair in the hash.
names.store(1, "Jane")
The first parameter of the store method is the key and the second parameter is the value.
In the third script, we create a hash with the hash literal notation. The values are bound by the curly brackets. And the key-value pairs are associated with the => characters.
#!/usr/bin/ruby
domains = { "de" => "Germany",
"sk" => "Slovakia",
"hu" => "Hungary",
"us" => "United States",
"no" => "Norway"
}
puts domains["de"]
puts domains["sk"]
We create a domains hash with 5 pairs. This time both keys and values are string types.
domains = { "de" => "Germany",
"sk" => "Slovakia",
"hu" => "Hungary",
"us" => "United States",
"no" => "Norway"
}
This is a hash literal notation. The key-value pairs are put between the curly brackets. The items are separated by the comma character. And the keys are associated with values using the => characters combination.
puts domains["de"]
Here we print the domain value name associated with the "de" key.
$ ./create3.rb
Germany
Slovakia
Output of the code example.
Basic work
In this section, we will present some methods for the very basic work with Ruby hashes.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
names[3] = "Robert"
names[4] = "Julia"
names[5] = "Rebecca"
puts "The size of the hash is #{names.size}"
puts names.keys.inspect
puts names.values.inspect
In the above Ruby script, we create a hash with five values. We introduce three hash methods.
puts "The size of the hash is #{names.size}"
The size method returns the size of the hash. It is a synonym for the length method.
puts names.keys.inspect
puts names.values.inspect
The keys method returns all keys of the hash. In a similar fashion, the values method returns all the values of the hash. The returned data is in the form of an array. To have a more readable output, we also call the inspect method on the returned arrays.
$ ./basic.rb
The size of the hash is 5
[1, 2, 3, 4, 5]
["Jane", "Thomas", "Robert", "Julia", "Rebecca"]
Ouput of the example. Note that the output of the last two methods are two arrays.
The second example of the section presents three distinct hash methods.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names1[3] = "Robert"
names1[4] = "Julia"
names1[5] = "Rebecca"
names2 = names1.dup
puts names1.eql? names2
puts names1.empty?
names1.clear
puts names1.empty?
The Ruby script creates a names hash. It calls three hash methods on the object.
names2 = names1.dup
We create a duplicate of the hash by calling the dup method. The method is inherited by the hash from the parent object.
puts names1.eql? names2
The eql? method compares two hash objects. In our case the hashes are equal and the line prints true.
puts names1.empty?
The empty? method checks whether the hash is empty or not. The line print false. Because the names1 hash has five items.
names1.clear
puts names1.empty?
The clear method deletes all items from the hash. The successive call of the empty? method returns true.
$ ./basic2.rb
true
false
true
Example output.
We have methods that can determine, whether a key or a value is present in the hash.
#!/usr/bin/ruby
domains = { :de => "Germany", :sk => "Slovakia",
:no => "Norway", :us => "United States"
}
puts domains.has_key? :de
puts domains.include? :no
puts domains.key? :me
puts domains.member? :sk
puts domains.has_value? "Slovakia"
puts domains.value? "Germany"
We create a domains hash with four pairs. The keys are symbols. Symbols are often used as keys, because they are more efficient.
puts domains.has_key? :de
puts domains.include? :no
puts domains.key? :me
puts domains.member? :sk
Here we have four methods that determine, whether a key is in the hash. They all do the same; they are synonyms.
puts domains.has_value? "Slovakia"
puts domains.value? "Germany"
These two methods check if the two strings are inside the hash.
$ ./has.rb
true
true
false
true
true
true
This is the output of the example.
In the final example of the section, we will read values from the hash.
#!/usr/bin/ruby
stones = { 1 => "garnet", 2 => "topaz",
3 => "opal", 4 => "amethyst"
}
puts stones.fetch 1
puts stones[2]
puts stones.values_at 1, 2, 3
The Ruby script presents three hash methods for reading values of a hash.
puts stones.fetch 1
The fetch method reads a value for a given key.
puts stones[2]
Square brackets can be used to get a value. In our case, the line prints "topaz" to the console.
puts stones.values_at 1, 2, 3
The values_at method can be used to get multiple values at one step. The method returns an array of the values for the given keys.
$ ./read.rb
garnet
topaz
garnet
topaz
opal
Output of the example.
Looping through a hash
There are several methods that can be used to loop through a Ruby hash.
#!/usr/bin/ruby
stones = { 1 => "garnet", 2 => "topaz",
3 => "opal", 4 => "amethyst"
}
stones.each { |k, v| puts "Key: #{k}, Value: #{v}" }
stones.each_key { |key| puts "#{key}" }
stones.each_value { |val| puts "#{val}" }
stones.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
In the above example, we present four methods. We use them to display all keys, values and both keys and values of a hash.
stones.each { |k, v| puts "Key: #{k}, Value: #{v}" }
The each method calls the given block for each key in the hash, passing key-value pair as parameter.
stones.each_key { |key| puts "#{key}" }
We use the each_key method to loop throug all keys of a hash. They are printed to the console.
stones.each_value { |val| puts "#{val}" }
The each_value can be used to loop throug the values of a hash.
stones.each_pair { |k, v| puts "Key: #{k}, Value: #{v}" }
The each_pair method is a synonym for the each method. We loop through the keys and values of the stones hash.
$ ./loop.rb
Key: 1, Value: garnet
Key: 2, Value: topaz
Key: 3, Value: opal
Key: 4, Value: amethyst
1
2
3
4
garnet
topaz
opal
amethyst
Key: 1, Value: garnet
Key: 2, Value: topaz
Key: 3, Value: opal
Key: 4, Value: amethyst
The output shows the keys and values, keys, values of the stones hash.
Deleting pairs
In the following examples, we will concern ourselves with methods that delete pairs from the hashes. There are methods that delete only a pair at a time and methods, that can delete multiple key-values at one step.
#!/usr/bin/ruby
names = Hash.new
names[1] = "Jane"
names[2] = "Thomas"
names[3] = "Robert"
names[4] = "Julia"
names[5] = "Rebecca"
names.delete 4
names.shift
puts names
In the script we have two methods. The delete method and the shift method. The delete method removes and returns a value for a specified key. The shift method deletes the first pair from the hash. It also returns the removed pair as an array.
names.delete 4
Here we delete a pair 4 => "Julia".
names.shift
This code line removes the first pair, namely 1 => "Jane".
$ ./deleteitem.rb
{2=>"Thomas", 3=>"Robert", 5=>"Rebecca"}
In the output we can see the pairs of the hash that are left.
The reject and the delete_if methods can remove multiple pairs from a hash. The methods delete pairs that return true for the given condition in the block. There is an important distinction between the two methods. The reject method works on a copy of a hash while the delete_if works on the original hash.
#!/usr/local/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names1[3] = "Robert"
names1[4] = "Julia"
names1[5] = "Rebecca"
puts names1.reject { |k, v| v =~ /R.*/ }
puts names1
puts names1.delete_if { |k, v| k<=3 }
puts names1
The example deletes multiple pairs using the previously mentioned methods.
puts names1.reject { |k, v| v =~ /R.*/ }
The reject method removes all values that fit the regular expression in the block. The modified hash is returned and the original hash is not changed.
puts names1
The output of this line confirms, that the original hash was intact.
puts names1.delete_if { |k, v| k<=3 }
In this case, we delete all pairs, for which the key is lower or equal to 3. The method modifies the original hash.
$ ./massdelete.rb
{1=>"Jane", 2=>"Thomas", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia", 5=>"Rebecca"}
{4=>"Julia", 5=>"Rebecca"}
{4=>"Julia", 5=>"Rebecca"}
Output of the example.
Adding hashes
Ruby has methods for hash addition. The merge and the update methods.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names2 = Hash.new
names2[3] = "Robert"
names2[4] = "Julia"
names = names1.merge names2
puts names
names = names1.update names2
puts names
In the Ruby script, we create two hashes. Then we apply merge and update methods on them.
names = names1.merge names2
puts names
We add two hashes, the names1 and the names2 and the result is assigned to the names hash. We print the newly created hash.
$ ./merge.rb
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
As we can see, the final hashes contain pairs from the names1 and names2 hashes.
The merge vs merge!
In the final section, we recap a common ruby thing. Several methods have their counterparts, that end with an explamation mark. The mark has no syntactical meaning. It is a Ruby idiom. It hints a programmer, that the method will actually modify an object, on which the method is called.
#!/usr/bin/ruby
names1 = Hash.new
names1[1] = "Jane"
names1[2] = "Thomas"
names2 = Hash.new
names2[3] = "Robert"
names2[4] = "Julia"
names = names1.merge names2
puts names
puts names1
names = names1.merge! names2
puts names
puts names1
We will demonstrate the difference on the merge and merge! methods.
names = names1.merge names2
The merge does not modify the names1 hash. It works on its copy.
names = names1.merge! names2
The merge! method works on the original hash. The names1 hash is changed.
$ ./merge2.rb
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
{1=>"Jane", 2=>"Thomas", 3=>"Robert", 4=>"Julia"}
相关推荐
### Ruby中关于Hash的基本使用方法 #### 一、哈希的概念 哈希(Hash)是一种存储键值对的数据结构,在Ruby中,哈希允许我们通过键(Key)来访问值(Value)。与数组不同,数组的索引是整数型的,而哈希的键可以是...
Ruby Hash类扩展。 安装 gem install ds_hash 通过实例学习 哈希。 deep_key? hash = { :a => { :b => 'b' } } hash . deep_key? :a , :b # return true hash . deep_key? :a , :x # return false 哈希。 deep_...
在Ruby编程语言中,Hash是一种极其重要的数据结构,它提供了键值对的存储方式,使得通过键(key)快速访问对应的值(value)成为可能。本文将深入探讨Ruby中Hash的基本操作方法。 首先,理解Hash的基本概念至关重要...
数据类型:Ruby支持多种数据类型,包括整数(Integer)、浮点数(Float)、字符串(String)、数组(Array)和哈希表(Hash)等。 运算符:Ruby支持常见的算术运算符(如+、-、*、/)、比较运算符(如==、!=、...
ruby-hash-syntax.el 改编自 TextMate 使用的方法,该库提供了一个命令ruby-hash-syntax-toggle ,它尝试在 1.8 和 1.9 哈希样式之间自动转换选定的 ruby 代码区域。 安装 如果您选择不使用方便的包之一,则...
这个 Ruby gem 向 Nokogiri XML 节点添加了一个to_hash方法,允许我们将任意 XML 节点转换为 Ruby 哈希,并且还可以将它们序列化为 JSON。 这个 gem 还可以获取属性、处理指令和文档类型声明。 由此产生的哈希是...
下面是关于Ruby中Hash哈希类型的一些基本操作方法的详细解释: 1. 创建哈希: - `Hash.new` 用于创建一个新的空哈希,默认值为 `nil`。 - `Hash.new("This is my first hash instance")` 创建一个空哈希,其...
这本面向初学者和高级读者的指南旨在全面介绍Ruby编程语言的基础及其高级特性,因此我们将从多个角度来解析这些内容。 ### 一、Ruby编程语言简介 #### 1.1 Ruby语言的历史与发展 Ruby是一种动态、面向对象的脚本...
这部分通常是对Ruby内置类库、方法、语法的详细参考,涵盖了标准库的所有模块和类,比如Array、Hash、String等。它提供了每个方法的用法、参数和返回值,是开发者在编写代码时查找特定功能或方法的重要资源。 3. *...
### Ruby 基本语言介绍 #### 一、Ruby 语言概述 Ruby 是一种简洁、高效且具有强大功能的编程语言。它最初由日本程序员松本行弘(Yukihiro Matsumoto)于1995年设计并开发。Ruby 的设计理念强调程序员的生产力和...
例如,它会详细介绍如何创建和使用类、继承机制、模块混入、块和 Proc 对象,以及符号(Symbol)、字符串(String)、数组(Array)、哈希(Hash)等基本数据类型的使用。核心库的理解对于掌握Ruby语言至关重要。 ...
Ohm也支持更复杂的Redis数据结构,如列表(List)、有序集合(Sorted Set)和哈希(Hash)。例如,你可以这样定义一个包含用户帖子的有序集合: ```ruby class Post attribute :title sorted_set :posts_by_date...
4. `hash_usage.rb`:展示了Ruby哈希(Hash)的使用,哈希是一种键值对的数据结构。 ```ruby person = {"name" => "Bob", "age" => 30} puts "Name: #{person["name"]}, Age: #{person["age"]}" ``` 5. `file_io.rb...
它包含了各种内置模块和类,如`Array`、`String`、`Hash`等,以及一些实用工具,如`irb`(交互式Ruby shell)和`rdoc`(用于生成API文档的工具)。 绿化Ruby193的过程通常涉及以下步骤: 1. 下载Ruby193的完整源码或预...
hash = {"name" => "Ruby", "age" => 20} ``` 3. **流程控制:** 包括条件语句(if...elsif...else)、循环语句(while...do...end、for...in...do...end)等。 ```ruby if age > 18 puts "成年人" elsif age ...
Ruby2JSON转换器简单的在线工具,可将ruby JSON /哈希控制台输出转换为正确的JSON。 例如{"ruby"=>"json"}转换为{ "ruby" : "json" }工作流程解析器非常幼稚。 解析过程如下: 拆下线制动器,将线连接成单线。 翻译=...
今天学习的是数组和Hash,数组在很多编程语言中都能常常用到。 数组: 数组是存储数据的一种容器,在Ruby中,数组中存储的数据可以是任何类型的数据;这和JAVA不同,在JAVA中,数组是存储同一类型数据的结构。 1. 在...
在Ruby编程语言中,读取配置文件(如cfg文件)是一项常见的任务,这通常涉及到从文件中获取特定的键值对以供程序使用。在本篇文章中,我们将深入探讨如何在Ruby中有效地读取和处理cfg文件,以及如何根据指定的键来...
- **核心类库**:探讨 Ruby 的核心类库,包括 Array、Hash、String 等类的功能及用法。 - **常用方法**:总结常用的核心类方法,如 map、select、each 等。 #### 3.2 标量对象 - **数值类型**:讲解数值类型(如 ...