Difference between "puts value" and "puts my_hash[key]"


#1

Using the code from the example:

matz = { "First name" => "Yukihiro",
  "Last name" => "Matsumoto",
  "Age" => 47,
  "Nationality" => "Japanese",
  "Nickname" => "Matz"
}

what is the difference between

matz.each do |key, value|
    puts matz[key]
end

and

matz.each do |key, value|
    puts value
end

is this a kind of good practice? or is indistinct whichever I use?
thanks in advance


#2

Either is fine. Consider that accessing by index permits mutation of the member value, where the value variable is immutable. This more or less hints at their specific roles. For printing, puts value is adequate enough.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.