Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3264

Re: using hash keys as object name

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: using hash keys as object name
Date 2011-04-20 13:15 -0500
Organization Service de news de lacave.net
Message-ID <5d4ed49cc2f3ecbed089fee592f19b80@ruby-forum.com> (permalink)
References <BANLkTik89Fy4Gr0BmpFGCGjMqEzsgJtAbg@mail.gmail.com>

Show all headers | View raw


Neubyr Neubyr wrote in post #993923:
> How do I assign hash's key as an object name?  For example I have a hash
> as:
>
>   fruits = {"apple" => ["green", "red"], "melons" => ["water", "musk"] }
>
> I would like to create a File or String objects by reading above hash,
> something like
>
>   fruits.keys.each {|k| "#{k}" = String.new}
>
> Any hints or suggestions on how to do this will be really helpful.
>
> thanks,
> neuby.

fruits = {"apple" => ["green", "red"], "melons" => ["water", "musk"] }

h = {}

fruits.keys.each do |key|
  h[key] = ""
end

p h

--output:--
{"apple"=>"", "melons"=>""}


h['apple'] = 10
p h

--output:--
{"apple"=>10, "melons"=>""}

--output:--

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

using hash keys as object name neubyr <neubyr@gmail.com> - 2011-04-20 01:07 -0500
  Re: using hash keys as object name Robert Klemme <shortcutter@googlemail.com> - 2011-04-20 02:36 -0500
    Re: using hash keys as object name neubyr <neubyr@gmail.com> - 2011-04-20 16:19 -0500
      Re: using hash keys as object name Robert Klemme <shortcutter@googlemail.com> - 2011-04-21 02:23 -0500
        Re: using hash keys as object name neubyr <neubyr@gmail.com> - 2011-04-22 20:40 -0500
  Re: using hash keys as object name 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-20 13:15 -0500

csiph-web