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


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]


matz.each do |key, value|
    puts value

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


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.


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