"nil"
In many languages, you'll get an error of some kind. Not so in Ruby: you'll instead get the special value nil.
Along with false, nil is one of two non-true values in Ruby. (Every other object is regarded as "truthy," meaning that if you were to type if 2 or if "bacon", the code in that if statement would be run.)
It's important to realize that false and nil are not the same thing: false means "not true," while nil is Ruby's way of saying "nothing at all."
example
creatures = { "weasels" => 0,
"puppies" => 6,
"platypuses" => 3,
"canaries" => 1,
"Heffalumps" => 7,
"Tiggers" => 1
}
creatures["amweoifna"]
will produce nil.
creating your own "nil"
no_nil_hash = Hash.new("nonillllllll") will replace "nil" with "nonillllllll" as a default value.
Object ID
object ID is an equivalent of hash in java.
The .object_id method gets the ID of an object—it's how Ruby knows whether two objects are the exact same object.
Symbol
You can think of a Ruby symbol as a sort of name.
Symbols always start with a colon (:). They must be valid Ruby variable names, so the first character after the colon has to be a letter or underscore (_); after that, any combination of letters, numbers, and underscores is allowed.
Symbols make good hash keys for a few reasons:
declare a symbol:
my_first_symbol = :symbolName
to_sym method:
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
# Add your code below!
symbols= []
strings.each { |s| symbols.push(s.to_sym) }
intern method:
Besides using .to_sym, you can also use .intern. This will internalize the string into a symbol and works just like .to_sym.
"toBeSymbolSoon".intern
Syntax changed from 1.9:
new_hash = { one: 1, two: 2, three: 3 }
no more : at the front. attach it to the back.
each_value & each_key method
In many languages, you'll get an error of some kind. Not so in Ruby: you'll instead get the special value nil.
Along with false, nil is one of two non-true values in Ruby. (Every other object is regarded as "truthy," meaning that if you were to type if 2 or if "bacon", the code in that if statement would be run.)
It's important to realize that false and nil are not the same thing: false means "not true," while nil is Ruby's way of saying "nothing at all."
example
creatures = { "weasels" => 0,
"puppies" => 6,
"platypuses" => 3,
"canaries" => 1,
"Heffalumps" => 7,
"Tiggers" => 1
}
creatures["amweoifna"]
will produce nil.
creating your own "nil"
no_nil_hash = Hash.new("nonillllllll") will replace "nil" with "nonillllllll" as a default value.
Object ID
object ID is an equivalent of hash in java.
The .object_id method gets the ID of an object—it's how Ruby knows whether two objects are the exact same object.
Symbol
You can think of a Ruby symbol as a sort of name.
Symbols always start with a colon (:). They must be valid Ruby variable names, so the first character after the colon has to be a letter or underscore (_); after that, any combination of letters, numbers, and underscores is allowed.
Symbols make good hash keys for a few reasons:
- They're immutable, meaning they can't be changed once they're created;
- Only one copy of any symbol exists at a given time, so they save memory;
- Symbol-as-keys are faster than strings-as-keys because of the above two reasons.
declare a symbol:
my_first_symbol = :symbolName
to_sym method:
strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
# Add your code below!
symbols= []
strings.each { |s| symbols.push(s.to_sym) }
intern method:
Besides using .to_sym, you can also use .intern. This will internalize the string into a symbol and works just like .to_sym.
"toBeSymbolSoon".intern
Syntax changed from 1.9:
new_hash = { one: 1, two: 2, three: 3 }
no more : at the front. attach it to the back.
each_value & each_key method