Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Constructing a Hash from a String?

Reply
Thread Tools

Constructing a Hash from a String?

 
 
Geoff
Guest
Posts: n/a
 
      05-12-2006
So I'm looking to create a Hash from a string which is pretty much
formatted like you would create a hash:

theString = "foo=>1,bar=>2,baz=>'go fish'"

and i was wondering if anyone had any nice and simple ideas for how to
grab this out. I came up with a little way to do it but i was just
wondering if anyone out there can think of a shorter way.

Here's what i came up with:

login = Hash.new
theString.split(',').each {|pair| pair.match(/(.+)=>(.+)/);
login[$1.intern]=$2 }

 
Reply With Quote
 
 
 
 
mojombo@gmail.com
Guest
Posts: n/a
 
      05-12-2006
Geoff wrote:
> So I'm looking to create a Hash from a string which is pretty much
> formatted like you would create a hash:
>
> theString = "foo=>1,bar=>2,baz=>'go fish'"
>
> and i was wondering if anyone had any nice and simple ideas for how to
> grab this out. I came up with a little way to do it but i was just
> wondering if anyone out there can think of a shorter way.


This is a bit obfuscational, but it works:

login = eval('{' + theString.split(',').map{|m| ':' + m}.join(',') +
'}')

Tom

 
Reply With Quote
 
 
 
 
Ross Bamford
Guest
Posts: n/a
 
      05-12-2006
On Fri, 12 May 2006 18:43:45 +0100, Geoff <(E-Mail Removed)> wrote:

> So I'm looking to create a Hash from a string which is pretty much
> formatted like you would create a hash:
>
> theString = "foo=>1,bar=>2,baz=>'go fish'"
>
> and i was wondering if anyone had any nice and simple ideas for how to
> grab this out. I came up with a little way to do it but i was just
> wondering if anyone out there can think of a shorter way.
>
> Here's what i came up with:
>
> login = Hash.new
> theString.split(',').each {|pair| pair.match(/(.+)=>(.+)/);
> login[$1.intern]=$2 }
>


Depends on exactly how you want the Hash, but maybe you could do:

h = Hash[*the_string.split(',').map { |e| e.split('=>') }.flatten]
# => {"baz"=>"'go fish'", "foo"=>"1", "bar"=>"2"}

or:

h = Hash[*the_string.split(',').map do |e|
e.split('=>').inject { |k,v| [k.intern,eval(v)] }
end.flatten]
# => {:foo=>1, :bar=>2, :baz=>"go fish"}

Unfortunately, if you want the second style I think you're stuck with eval
and it's (deserved) reputation as slow and insecure.

--
Ross Bamford - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
rcoder
Guest
Posts: n/a
 
      05-12-2006
I'd recommend staying away from using the Ruby hash syntax, unless you
also want to implement a parser for Ruby strings -- your simple version
will break if any values in your hash include a comma. For example:

theString = "foo=>1,bar=>2,baz=>'go fish, then go home'"

Your split will find the comma in the value associated with baz, but
the search for '=>' will fail, resulting in a truncated value for the
baz key.

Have you considered using YAML, or another non-Ruby syntax for the
hashes?

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      05-15-2006
YAML could be an option. I'll look in to it.

Thanks for the suggestions.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Constructing a Hash from a function and its domain Tim L Ruby 1 04-12-2006 10:54 AM
Help constructing interesting hash? Chris McMahon Ruby 9 12-01-2005 11:11 PM
access webcontrol dynamically from code behind - by constructing name Leo Muller ASP .Net 2 06-16-2004 01:16 PM



Advertisments