[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Convert String to Hash

Nick Snels

3/9/2006 11:53:00 AM

Hi,

I get a string inputted in my program, which I convert to a hash-like
string, like:

sql = '"table_alias" => [],"original_string" => "select * from table"'

The string has the form of a hash, now I would like to convert this
string to an actual hash. I tried it like:

test = Hash[sql]
or
test = Hash.[](sql)

but I get the following error:
test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
from test2.rb:2

When I do it like,

test = Hash.new
test = sql

test becomes a String and is no longer a Hash.

Is there an easy way to convert my hash-like string to an actual hash?
Thanks for any help.

Kind regards,

Nick

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


9 Answers

Bernhard 'elven' Stoeckner

3/9/2006 11:57:00 AM

0

Nick Snels scribbled on Thursday 09 Mar 2006 12:52:
> Hi,
>
> I get a string inputted in my program, which I convert to a hash-like
> string, like:
>
> sql = '"table_alias" => [],"original_string" => "select * from table"'
>
> The string has the form of a hash, now I would like to convert this
> string to an actual hash. I tried it like:
>
> test = Hash[sql]
> or
> test = Hash.[](sql)
>
> but I get the following error:
> test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
> from test2.rb:2
>
> When I do it like,
>
> test = Hash.new
> test = sql
>
> test becomes a String and is no longer a Hash.
>
> Is there an easy way to convert my hash-like string to an actual hash?
> Thanks for any help.
>
> Kind regards,
>
> Nick

Hi Nick,

no, you can't pass it to Hash.new directly, you'd have to write a parser for
it yourself. If you're feeling lucky, you can use eval() to do that.

*takes cover from flying bananas and such*

Bernhard

Rob Pitt

3/9/2006 12:01:00 PM

0

Use YAML.

On 9 Mar 2006, at 11:52, Nick Snels wrote:

> Hi,
>
> I get a string inputted in my program, which I convert to a hash-like
> string, like:
>
> sql = '"table_alias" => [],"original_string" => "select * from table"'
>
> The string has the form of a hash, now I would like to convert this
> string to an actual hash. I tried it like:
>
> test = Hash[sql]
> or
> test = Hash.[](sql)
>
> but I get the following error:
> test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
> from test2.rb:2
>
> When I do it like,
>
> test = Hash.new
> test = sql
>
> test becomes a String and is no longer a Hash.
>
> Is there an easy way to convert my hash-like string to an actual hash?
> Thanks for any help.
>
> Kind regards,
>
> Nick
>
> --
> Posted via http://www.ruby-....
>



Rob Pitt

3/9/2006 12:02:00 PM

0

*throws banana ad Bernhard*

no no no don't eval user supplied strings you are asking to be hacked :)

On 9 Mar 2006, at 11:58, Bernhard 'elven' Stoeckner wrote:

> Nick Snels scribbled on Thursday 09 Mar 2006 12:52:
>> Hi,
>>
>> I get a string inputted in my program, which I convert to a hash-like
>> string, like:
>>
>> sql = '"table_alias" => [],"original_string" => "select * from
>> table"'
>>
>> The string has the form of a hash, now I would like to convert this
>> string to an actual hash. I tried it like:
>>
>> test = Hash[sql]
>> or
>> test = Hash.[](sql)
>>
>> but I get the following error:
>> test2.rb:2:in `[]': odd number of arguments for Hash (ArgumentError)
>> from test2.rb:2
>>
>> When I do it like,
>>
>> test = Hash.new
>> test = sql
>>
>> test becomes a String and is no longer a Hash.
>>
>> Is there an easy way to convert my hash-like string to an actual
>> hash?
>> Thanks for any help.
>>
>> Kind regards,
>>
>> Nick
>
> Hi Nick,
>
> no, you can't pass it to Hash.new directly, you'd have to write a
> parser for
> it yourself. If you're feeling lucky, you can use eval() to do that.
>
> *takes cover from flying bananas and such*
>
> Bernhard
>



Marcin Mielzynski

3/9/2006 12:02:00 PM

0

Nick Snels wrote:
> Hi,
>
> I get a string inputted in my program, which I convert to a hash-like
> string, like:
>
> sql = '"table_alias" => [],"original_string" => "select * from table"'
>

The simplest, although slow:

sql = '"table_alias" => [],"original_string" => "select * from table"'
hash = eval('{'+sql+'}')


lopex

Bernhard 'elven' Stoeckner

3/9/2006 12:06:00 PM

0

Rob Pitt scribbled on Thursday 09 Mar 2006 13:01:

> *throws banana ad Bernhard*
>
> no no no don't eval user supplied strings you are asking to be hacked :)

Well, in certain local-only low-security scenarios it might be of use. I
wouldn't know where Nick takes his strings from. :}

Rob Pitt

3/9/2006 12:08:00 PM

0

Argh!

Please stop telling people to eval user supplied strings!

The *ONLY* time it is safe to use eval is if all the contents are
generated by you and NONE of the components come from user input.

It looks here like original_string comes from user.

Imagine if I supplied this:

" => 'lame'; `rm -rf /` #

On 9 Mar 2006, at 12:03, Marcin Mielzynski wrote:

> Nick Snels wrote:
>> Hi,
>> I get a string inputted in my program, which I convert to a hash-
>> like string, like:
>> sql = '"table_alias" => [],"original_string" => "select * from
>> table"'
>
> The simplest, although slow:
>
> sql = '"table_alias" => [],"original_string" => "select * from table"'
> hash = eval('{'+sql+'}')
>
>
> lopex
>



Nick Snels

3/9/2006 12:41:00 PM

0

Hi,

thanks for all the replies. I used the eval method and it worked!! The
string doesn't come directly from user input. It first goes through a
Perl sql parser and then into my program. So hopefully this doesn't
introduce any security issues, but I will be aware of the possibility.
Thanks for the help.

Kind regards,

Nick

Bernhard 'elven' Stoeckner wrote:
> Rob Pitt scribbled on Thursday 09 Mar 2006 13:01:
>
>> *throws banana ad Bernhard*
>>
>> no no no don't eval user supplied strings you are asking to be hacked :)
>
> Well, in certain local-only low-security scenarios it might be of use. I
> wouldn't know where Nick takes his strings from. :}


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


plainolamerican

9/24/2013 4:21:00 PM

0

On Tuesday, September 24, 2013 4:28:12 AM UTC-5, The Peeler wrote:
> On Tue, 24 Sep 2013 10:10:37 +0200, the Dutch pig oinked:
>
>
>
> > Scottish police have expressed dismay that Jewish community members see
>
> > antisemitism as “normal”.
>
>
>
> Yes, I've observed that too, that Jews are usually too gentle, humble and
>
> unassertive with goy Nazis. Just look how they let PIGS like you have their
>
> way on Usenet, while some of the "goys" will deal with you Nazi rats as you
>
> deserve it!
>
>
>
>
>
> --
>
> Dumb Dutch Nazi Heini's motto: "to suck rectum or not to suck rectum that is
>
> the question"
>
> MID: <4db7abbb$0$24816$2e0edba0@news.tweakdsl.nl>

Jews are usually too gentle, humble and
unassertive with goy Nazis. Just look how they let PIGS like you have their
way on Usenet
---
as if jews own Usenet.

The Peeler

9/24/2013 4:55:00 PM

0

On Tue, 24 Sep 2013 09:20:59 -0700 (PDT), plainstupidamerican wrote:


>> Dumb Dutch Nazi Heini's motto: "to suck rectum or not to suck rectum that is
>>
>> the question"
>>
>> MID: <4db7abbb$0$24816$2e0edba0@news.tweakdsl.nl>
>
> Jews are usually too gentle, humble and
> unassertive with goy Nazis. Just look how they let PIGS like you have their
> way on Usenet
> ---
> as if jews own Usenet.

They COULD do something about your sort, if they REALLY wanted! Sadly, they
only STARTED reacting to the online Nazi scum lately, when they successfully
dealt with the Nazi scum on Twitter and on Google! I don't doubt that sooner
or later they will also handle the Usenet Nazi scum! <BG>