ruby method to convert a hash to a string -
please me make method named hash_string takes hash , make string key/value pairs. example:
hash_string({id: 8, name: 'marry'}) # should return "id = 8, name = marry"
i've tried same way before, are:
def hash_string(hash) hash.to_s end
and
def hash_string(hash) set_value = hash.each {|key, value| puts "#{key} = #{value}" } # set_value.join(",") end
but did not work. please explain me, how can that?
replace each
map
, return value instead of outputting it, , you're done.
def hash_string(hash) set_value = hash.map {|key, value| "#{key} = #{value}" } set_value.join(",") end
Comments
Post a Comment