Passing hashes as command-line parameters

Discussion in 'Ruby' started by Jeff Leeman, Apr 16, 2009.

  1. Jeff Leeman

    Jeff Leeman Guest

    Is it not possible to pass hashes as command-line arguments to a Ruby
    script?

    Thanks,
    Jeff
    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Leeman, Apr 16, 2009
    #1
    1. Advertising

  2. Jeff Leeman wrote:
    > Is it not possible to pass hashes as command-line arguments to a Ruby
    > script?


    Like ... ?

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 16, 2009
    #2
    1. Advertising

  3. Jeff Leeman

    Jeff Leeman Guest

    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 ... ?


    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Leeman, Apr 16, 2009
    #3
  4. 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 Hungerecker, Apr 16, 2009
    #4
  5. Jeff Leeman

    Jeff Leeman Guest

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

    Thanks,
    Jeff


    Sebastian Hungerecker 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Leeman, Apr 16, 2009
    #5
  6. 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.
     
    Sebastian Hungerecker, Apr 16, 2009
    #6
  7. 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'})"
    >
    > Joel VanderWerf wrote:
    >> Jeff Leeman wrote:
    >>> Is it not possible to pass hashes as command-line arguments to a Ruby
    >>> script?

    >> Like ... ?

    >


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

    $ ruby -e 'p eval(ARGV[0])' '{:foo => :bar}'
    {:foo=>:bar}

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
     
    Joel VanderWerf, Apr 16, 2009
    #7
  8. Jeff Leeman

    Jeff Leeman Guest

    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}


    --
    Posted via http://www.ruby-forum.com/.
     
    Jeff Leeman, Apr 16, 2009
    #8
  9. 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.
     
    Sebastian Hungerecker, Apr 17, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    593
    Ben Holness
    Oct 8, 2003
  2. Jonck van der Kogel
    Replies:
    2
    Views:
    1,011
    Jonck van der Kogel
    May 27, 2004
  3. Replies:
    4
    Views:
    441
    Steven Bethard
    Sep 24, 2007
  4. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    186
    Mauricio Fernández
    Nov 23, 2005
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    237
Loading...

Share This Page