It sounds like failed_hosts
and ca_hosts_down
are just hashes. Hashes hold key and value pairs. The keys are always unique. If you store a duplicate key, you will see the previous value is discarded. Also, hashes are not for storing sequential data like Arrays. Here’s a sample hash:
a = {:hello => :hello.class, 5 => 5.class, true => true.class, 'hello' => 'hello'.class, ?A => ?A.class, x: :x.class}
p a.values # => [5, Integer, TrueClass, String, String, Symbol]
p a[:hello] # => Symbol
p a[true] # => TrueClass
p a[100.*(0).zero?] # => TrueClass # because 100 * 0 == 0
You can store any data in a hash and array. Both arrays and hashes are mutable objects. But rubyists prefer hash when to need to look up for an item - it’s optimized for that.
For sequential storage of duplicate data - it’s array that’s preferred. Arrays can iterate over items faster than hashes too!
.reject
and the .any?
are just methods on the hash.
There are two kind of blocks, the low precedence do ... end
block and the high precedence { }
block.
Every method can take a block in Ruby even if it’s not used.
def foo(&block)
block.call(1)
end
def bar() yield 1 end
def baz() 5 end
foo { |y| puts y } # output: 1 ; returns: nil
bar { |y| puts y } # output: 1 ; returns: nil
bar { |y| p y + y } # output: 2 returns: 2
baz { |y| puts y } # output: none, returns: 5
Blocks take argument in ||
. The |h|
contains a value yielded by the reject! method. The |c|
contains value yielded by the any?
method on each iteration.
Everything is an object in Ruby. The :host
is a symbol. You can write :Hello
or %s(Hello)
to make a symbol called :Hello
. Generally symbol objects are light weight, just like integer objects.
Speaking of reject!, generally the reject! is a part of Enumerator#reject!
. It’s called the reject bang
method because of the !
. It modifies the original object without changing it’s object_id. In other words, the memory location of the object isn’t changed, and thus, unlike =
assignment operator, it doesn’t copy the content of the hash from a memory location to another memory location and releasing the previous memory. Nevertheless, it’s way faster if you have a hash with millions of key - value pairs. There is also a reject
method without bang !
, which doesn’t modify the original hash.
BTW, here reject!
rejects all items from the failed_hosts
hash if ca_hosts_down
hash has one or more item that matches the key :host
and :binary
at the same time.
Hope this helps!