This is a great "Intro to Ruby" chapter. It is really helpful to follow along with the Rails Console just to get used to using it.
The map method returns the result of applying the given block to each element in the array or range.
I wish this was worded better
4.3.3 Hashes and symbolsVery cool
Hashes are essentially arrays that aren’t limited to integer indices. (In fact, some languages, especially Perl, sometimes call hashes associative arrays for this reason.) Instead, hash indices, or keys, can be almost any object.
flash = { success: "It worked!", error: "It failed." }methods
flash.each do |key, value|
puts "Key #{key.inspect} has value #{value.inspect}"
end
inspect
p -
p
returns the object being printed while puts
always returns nil
map
second - a rails method
Hash merge http://www.ruby-doc.org/core-2.1.2/Hash.html#method-i-merge
strings vs. hashes
strings need to be compared character by character, while symbols can be compared all in one go.
When hashes are the last argument in a function call, the curly braces are optional
Exercises
Create three hashes called person1, person2, and person3, with first and last names under the keys :first and :last. Then create a params hash so that params[:father] is person1, params[:mother] is person2, and params[:child] is person3. Verify that, for example, params[:father][:first] has the right value.
instance method
A method called on an instance, such as length, is called an instance method.
class method
When a method gets called on the class itself, as in the case of new, it’s called a class method
No comments:
Post a Comment