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 Wed, 20 Apr 2011 13:15:41 -0500
Organization Service de news de lacave.net
Lines 41
Message-ID <5d4ed49cc2f3ecbed089fee592f19b80@ruby-forum.com> (permalink)
References <BANLkTik89Fy4Gr0BmpFGCGjMqEzsgJtAbg@mail.gmail.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1303323361 20310 65.111.164.187 (20 Apr 2011 18:16:01 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Wed, 20 Apr 2011 18:16:01 +0000 (UTC)
In-Reply-To <BANLkTik89Fy4Gr0BmpFGCGjMqEzsgJtAbg@mail.gmail.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 381948
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <5d4ed49cc2f3ecbed089fee592f19b80@ruby-forum.com>
Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!feed.ac-versailles.fr!talisker.lacave.net!lacave.net!not-for-mail
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:3264

Show key headers only | 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