Ruby中的集合(Collections)

There are two traditional ways to create an array of elements. The first one is to list out a number of elements, surrounded by brackets, and store the array in a variable. In this first example, we're storing five integers in a variable calledx:

x = [12, 3, 454, 234, 234]
y = Array.new 
y[0] = 543

an array will fill empty spots if you don't provide the values for them.

y.each do |i| 
  puts i 
end

删除元素

y = Array.new

y[0] = 111
y[10] = 1123

traverse = lambda {|x| print x," "}

x = ["asdf", 3, 4, 12, "asdf", "b", true, 34, 2, 4, 4, 4]

traverse[x]

puts x.count
x.delete(4) #删除所有元素的值为4的元素
traverse[x]
puts
x.delete_at(3) #按照下表删除元素
traverse.call(x)
puts x.count

An important item to understand when using the delete_at method is that when you use this method, it not only deletes 12 but also returns it. So, if you ever need the value of the element that was deleted from the array, you can get it from this method. In fact, this is a practical function that you are sure to use while creating applications. For example, if you want to store a value that was removed from the array, I can do something like this.y = x.delete_at(4)

batting_averages = [0.3, 0.18, 0.22, 0.25]
batting_averages.delete_if { |average| average <0.24}

Push and Pop

traverse = lambda {|x| print x," "}

batting_averages = [0.3, 0.18, 0.22, 0.25]
batting_averages.delete_if { |average| average <0.24}
# traverse.call(batting_averages)

teams = ["astros", "yankees", "rangers", "mets", "cardinals"]
traverse.call(teams)
teams.push("marlins")
puts
 traverse[teams]
 puts
pop_elment = teams.pop()
puts pop_elment
traverse[teams] 
puts
teams.push("red sox", "blue jays")
traverse[teams]
z= teams.pop
puts z

Ruby 中的hash函数

Essentially, a hash is a key/value pair collection. In an array, each element is a single item such as a number or word whereas in a hash, each element has two items, and includes a value and a key associated with it.:本质上,散列是键/值对集合。在数组中,每个元素都是单个项,如数字或单词;而在散列中,每个元素都有两个项,并包含与其关联的值和键。,在编写的代码中,为了便于阅读,我将每个键/值对放在各自的行上;不过,您也可以使用irb控制台并在一行上构建散列。这一点很重要,因为Ruby解析器不关心空格,所以这两个选项都可以很好地工作。然而,当您构建基于代码的程序时,最好将每个元素放在它自己的行上.

Ruby has automatically inserted a colon in front of every position to denote that it's the key and it is mapped to a value with the => symbol, which is called a hash rocket:

positions = { 
                first_base: "Chris Carter", 
                second_base: "Jose Altuve", 
                short_stop: "Carlos Correa" 
            }
positions =  {
                    :first_base=>"Chris Carter", 
                    :second_base=>"Jose Altuve",
                    :short_stop=>"Carlos Correa"
}
positions = {
               "first_base" => "Chris Carter", 
               " second_base" => "Jose Altuve", 
               "short_stop" => "Carlos Correa" 
}

The easy way to remember how to grab items from a hash is that you use the key to look up the corresponding value, and you pass it in as a symbol. This feature is one of the reasons why hashes are so popular for developers

数组和哈希的不同之处

With arrays, if you want to grab an element from the collection, you either have to know its position in the array or you have to iterate through it until you find the item you want to select.

However, with hashes, we can look up items based on their key, which is more practical in many different cases. Having a clear understanding of the differences between hashes and arrays is important so you can decide when to use each type of data structure.

你可能感兴趣的:(Ruby中的集合(Collections))