Forum: Ruby Passing hashes as command-line parameters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jeff L. (Guest)
on 2009-04-16 23:10
Is it not possible to pass hashes as command-line arguments to a Ruby
script?

Thanks,
Jeff
Joel VanderWerf (Guest)
on 2009-04-16 23:38
(Received via mailing list)
Jeff Leeman wrote:
> Is it not possible to pass hashes as command-line arguments to a Ruby
> script?

Like ... ?
Jeff L. (Guest)
on 2009-04-17 00:36
Well, what I'm looking for is to be able run something like:

ruby script/runner "SomeClassName.method_name({:some_key_1 =>
'some_value_1', :some_key_2 => 'some_value_2', :some_key_3 =>
'some_value_3'})"

Joel VanderWerf wrote:
> Jeff Leeman wrote:
>> Is it not possible to pass hashes as command-line arguments to a Ruby
>> script?
>
> Like ... ?
Sebastian H. (Guest)
on 2009-04-17 00:43
(Received via mailing list)
Jeff Leeman wrote:
> Well, what I'm looking for is to be able run something like:
>
> ruby script/runner "SomeClassName.method_name({:some_key_1 =>
> 'some_value_1', :some_key_2 => 'some_value_2', :some_key_3 =>
> 'some_value_3'})"

You're perfectly able to run something like that. See:


~> mkdir script
~> cat>script/runner
class SomeClassName
  def self.method_name(hash)
    p hash
  end
end
eval ARGV[0]

~> ruby script/runner "SomeClassName.method_name({:some_key_1
=> 'some_value_1', :some_key_2 => 'some_value_2', :some_key_3
=> 'some_value_3'})"
{:some_key_1=>"some_value_1", :some_key_2=>"some_value_2",
:some_key_3=>"some_value_3"}

HTH,
Sebastian
Jeff L. (Guest)
on 2009-04-17 01:00
Which OS are you using?  Also, are you running this from an IRC prompt
or a standard bash shell?

Thanks,
Jeff


Sebastian H. wrote:
> Jeff Leeman wrote:
>> Well, what I'm looking for is to be able run something like:
>>
>> ruby script/runner "SomeClassName.method_name({:some_key_1 =>
>> 'some_value_1', :some_key_2 => 'some_value_2', :some_key_3 =>
>> 'some_value_3'})"
>
> You're perfectly able to run something like that. See:
>
>
> ~> mkdir script
> ~> cat>script/runner
> class SomeClassName
>   def self.method_name(hash)
>     p hash
>   end
> end
> eval ARGV[0]
>
> ~> ruby script/runner "SomeClassName.method_name({:some_key_1
> => 'some_value_1', :some_key_2 => 'some_value_2', :some_key_3
> => 'some_value_3'})"
> {:some_key_1=>"some_value_1", :some_key_2=>"some_value_2",
> :some_key_3=>"some_value_3"}
>
> HTH,
> Sebastian
Sebastian H. (Guest)
on 2009-04-17 01:09
(Received via mailing list)
Jeff Leeman wrote:
> Which OS are you using?

Linux though that should really not make any difference. The ruby code I
posted will work on any platform (though you'd be using something other
than
cat to write the code into the file on systems without cat - but then
you'd
be doing that anyway, I only used cat so I could copy'n'paste the whole
thing
from my bash prompt).

> Also, are you running this from an IRC prompt
> or a standard bash shell?

bash. "~>" is my bash prompt.
Joel VanderWerf (Guest)
on 2009-04-17 02:12
(Received via mailing list)
Jeff Leeman wrote:
>> Like ... ?
>

You could just eval ARGV[0] in this case.

$ ruby -e 'p eval(ARGV[0])' '{:foo => :bar}'
{:foo=>:bar}
Jeff L. (Guest)
on 2009-04-17 02:28
Ok, interesting.  I'm using a PC, and I tried the exact code that
Sebastian put together, and it runs fine in Cygwin, but does not work in
the command prompt window:

C:\rails_app>jruby script\runner "SomeClassName.method_name({:some_key_1
=> 'some_value_1', :some_key_2 => 'some_value_2'})"
script\runner:6: (eval):1: , unexpected '=' (SyntaxError)

I guess I don't know the command prompt language too well, but there's
probably some special way things have to be delimited with quotes.

-Jeff


Joel VanderWerf wrote:
> Jeff Leeman wrote:
>>> Like ... ?
>>
>
> You could just eval ARGV[0] in this case.
>
> $ ruby -e 'p eval(ARGV[0])' '{:foo => :bar}'
> {:foo=>:bar}
Sebastian H. (Guest)
on 2009-04-17 12:40
(Received via mailing list)
Jeff Leeman wrote:
> C:\rails_app>jruby script\runner "SomeClassName.method_name({:some_key_1
> => 'some_value_1', :some_key_2 => 'some_value_2'})"
> script\runner:6: (eval):1: , unexpected '=' (SyntaxError)

Try putting  p ARGV[0] before the eval and see what that outputs. That
way you
can see if and how cmd butchers the argument.
This topic is locked and can not be replied to.