Ruby idiom equivalent to Perl "$asdf = $ARGV[0] || die 'argrequired'"?

Discussion in 'Ruby' started by Giles Bowkett, Dec 4, 2007.

  1. Giles Bowkett, Dec 4, 2007
    #1
    1. Advertising

  2. Giles Bowkett wrote:
    > I'm making some command-line utilities easier to use and I was
    > wondering if there's a really nice simple Rubyish way to do what I
    > used to do with Perl back in the day. All my code in these utils which
    > exit and print usage strings in the absence of required command-line
    > args is looking kinda clunky to me.
    >
    >


    Are you saying you _do_ want something like the example you gave?

    var = ARGV[0] || raise("Argument required")


    -Justin
     
    Justin Collins, Dec 4, 2007
    #2
    1. Advertising

  3. Giles Bowkett

    MonkeeSage Guest

    On Dec 4, 12:41 am, Giles Bowkett <> wrote:
    > I'm making some command-line utilities easier to use and I was
    > wondering if there's a really nice simple Rubyish way to do what I
    > used to do with Perl back in the day. All my code in these utils which
    > exit and print usage strings in the absence of required command-line
    > args is looking kinda clunky to me.
    >
    > --
    > Giles Bowkett
    >
    > Podcast:http://hollywoodgrit.blogspot.com
    > Blog:http://gilesbowkett.blogspot.com
    > Portfolio:http://www.gilesgoatboy.org
    > Tumblelog:http://giles.tumblr.com


    You can do something like this, but it's ugly (parens necessary)...

    asdf = ARGV[0] || (puts("argument required"); exit 1)

    Better...

    def die(message)
    puts message
    exit 1
    end

    asdf = ARGV[0] || die("argument required")

    But I would still probably write something like this instead...

    class Array
    def at!(index, message)
    if self.at(index)
    self.at(index)
    else
    puts message
    exit 1
    end
    end
    end

    asdf = ARGV.at!(0, "argument required")

    ....though I guess that's really a matter of preference. :)

    Regards,
    Jordan
     
    MonkeeSage, Dec 4, 2007
    #3
  4. Justin Collins wrote:
    > Giles Bowkett wrote:
    >> I'm making some command-line utilities easier to use and I was
    >> wondering if there's a really nice simple Rubyish way to do what I
    >> used to do with Perl back in the day. All my code in these utils which
    >> exit and print usage strings in the absence of required command-line
    >> args is looking kinda clunky to me.
    >>
    >>

    >
    > Are you saying you _do_ want something like the example you gave?
    >
    > var = ARGV[0] || raise("Argument required")
    >
    >
    > -Justin


    I guess you can get rid of the parentheses, too.

    var = ARGV[0] or raise "Argument required"
     
    Justin Collins, Dec 4, 2007
    #4
  5. Giles Bowkett

    Ryan Davis Guest

    On Dec 3, 2007, at 22:41 , Giles Bowkett wrote:

    > I'm making some command-line utilities easier to use and I was
    > wondering if there's a really nice simple Rubyish way to do what I
    > used to do with Perl back in the day. All my code in these utils which
    > exit and print usage strings in the absence of required command-line
    > args is looking kinda clunky to me.


    How ruby REALLY stands out... this can be read out loud:

    abort "arguments are required" if ARGV.empty?

    ARGV.each do |arg|
    # ...
    end
     
    Ryan Davis, Dec 4, 2007
    #5
  6. > > I'm making some command-line utilities easier to use and I was
    > > wondering if there's a really nice simple Rubyish way to do what I
    > > used to do with Perl back in the day. All my code in these utils which
    > > exit and print usage strings in the absence of required command-line
    > > args is looking kinda clunky to me.

    >
    > How ruby REALLY stands out... this can be read out loud:
    >
    > abort "arguments are required" if ARGV.empty?
    >
    > ARGV.each do |arg|
    > # ...
    > end


    abort!

    cool. that's better than die. I remember one time talking with a
    fellow Perl guy, figuring out what a program was doing, and I realized
    we had used the word "kill" like thirty times in a minute of
    discussion. "abort" sounds much better. like Star Trek.

    --
    Giles Bowkett

    Podcast: http://hollywoodgrit.blogspot.com
    Blog: http://gilesbowkett.blogspot.com
    Portfolio: http://www.gilesgoatboy.org
    Tumblelog: http://giles.tumblr.com
     
    Giles Bowkett, Dec 4, 2007
    #6
  7. > > I'm making some command-line utilities easier to use and I was
    > > wondering if there's a really nice simple Rubyish way to do what I
    > > used to do with Perl back in the day. All my code in these utils which
    > > exit and print usage strings in the absence of required command-line
    > > args is looking kinda clunky to me.

    >
    > Are you saying you _do_ want something like the example you gave?
    >
    > var = ARGV[0] || raise("Argument required")


    Yeah, because I'm not just a Rails programmer, so I know other
    languages have merit too.

    --
    Giles Bowkett

    Podcast: http://hollywoodgrit.blogspot.com
    Blog: http://gilesbowkett.blogspot.com
    Portfolio: http://www.gilesgoatboy.org
    Tumblelog: http://giles.tumblr.com
     
    Giles Bowkett, Dec 4, 2007
    #7
  8. Giles Bowkett, Dec 4, 2007
    #8
  9. Giles Bowkett

    MonkeeSage Guest

    On Dec 4, 10:31 am, Giles Bowkett <> wrote:
    > > > I'm making some command-line utilities easier to use and I was
    > > > wondering if there's a really nice simple Rubyish way to do what I
    > > > used to do with Perl back in the day. All my code in these utils which
    > > > exit and print usage strings in the absence of required command-line
    > > > args is looking kinda clunky to me.

    >
    > > Are you saying you _do_ want something like the example you gave?

    >
    > > var = ARGV[0] || raise("Argument required")

    >
    > Yeah, because I'm not just a Rails programmer, so I know other
    > languages have merit too.
    >
    > --
    > Giles Bowkett
    >
    > Podcast:http://hollywoodgrit.blogspot.com
    > Blog:http://gilesbowkett.blogspot.com
    > Portfolio:http://www.gilesgoatboy.org
    > Tumblelog:http://giles.tumblr.com


    raise/fail don't have perl compatibility...they print nasty stuff
    about errors and what-not. In perl, "die" just prints a message and
    dies (how suprising). If you really want something equiv. to the perl,
    you'll have to write your own #die or do something more extensive.

    Regards,
    Jordan
     
    MonkeeSage, Dec 4, 2007
    #9
  10. Giles Bowkett

    Tim Hunter Guest

    Re: Ruby idiom equivalent to Perl "$asdf = $ARGV|| die 'arg

    Giles Bowkett wrote:
    > cool. that's better than die. I remember one time talking with a
    > fellow Perl guy, figuring out what a program was doing, and I realized
    > we had used the word "kill" like thirty times in a minute of
    > discussion. "abort" sounds much better. like Star Trek.


    "abort" and "kill" are way too negative and violent. I use
    "succeed_at_alternate_goal" instead.

    --
    Posted via http://www.ruby-forum.com/.
     
    Tim Hunter, Dec 4, 2007
    #10
  11. Re: Ruby idiom equivalent to Perl "$asdf = $ARGV[0] || die 'arg required'"?

    On Dec 4, 2007, at 11:08 AM, MonkeeSage wrote:

    > raise/fail don't have perl compatibility...they print nasty stuff
    > about errors and what-not. In perl, "die" just prints a message and
    > dies (how suprising). If you really want something equiv. to the perl,
    > you'll have to write your own #die or do something more extensive.


    $ qri Kernel#abort
    ----------------------------------------------------------- Kernel#abort
    abort
    Kernel::abort
    Process::abort
    ------------------------------------------------------------------------
    Terminate execution immediately, effectively by calling
    Kernel.exit(1). If msg is given, it is written to STDERR prior to
    terminating.

    James Edward Gray II
     
    James Edward Gray II, Dec 4, 2007
    #11
  12. Giles Bowkett

    MonkeeSage Guest

    On Dec 4, 11:30 am, James Edward Gray II <>
    wrote:
    > On Dec 4, 2007, at 11:08 AM, MonkeeSage wrote:
    >
    > > raise/fail don't have perl compatibility...they print nasty stuff
    > > about errors and what-not. In perl, "die" just prints a message and
    > > dies (how suprising). If you really want something equiv. to the perl,
    > > you'll have to write your own #die or do something more extensive.

    >
    > $ qri Kernel#abort
    > ----------------------------------------------------------- Kernel#abort
    > abort
    > Kernel::abort
    > Process::abort
    > ------------------------------------------------------------------------
    > Terminate execution immediately, effectively by calling
    > Kernel.exit(1). If msg is given, it is written to STDERR prior to
    > terminating.
    >
    > James Edward Gray II


    I stand corrected. :)

    Regards,
    Jordan
     
    MonkeeSage, Dec 4, 2007
    #12
  13. Giles Bowkett

    Marc Heiler Guest

    Re: Ruby idiom equivalent to Perl "$asdf = $ARGV|| die 'arg

    > "abort" and "kill" are way too negative and violent.

    How about

    stop

    ;)
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Dec 4, 2007
    #13
  14. Giles Bowkett wrote:
    >>> Are you saying you _do_ want something like the example you gave?
    >>>
    >>> var = ARGV[0] || raise("Argument required")
    >>>

    >> Yeah, because I'm not just a Rails programmer, so I know other
    >> languages have merit too.
    >>

    >
    > Wait, sorry, that was lame of me.
    >
    >


    Yeah, I just wasn't sure what you wanted. Like a more object-oriented
    version or what.

    Looks like abort wins the day, though.

    var = ARGV[0] or abort "Arg required"

    -Justin
     
    Justin Collins, Dec 4, 2007
    #14
  15. > >>> Are you saying you _do_ want something like the example you gave?
    > >>>
    > >>> var = ARGV[0] || raise("Argument required")
    > >>>
    > >> Yeah, because I'm not just a Rails programmer, so I know other
    > >> languages have merit too.

    > >
    > > Wait, sorry, that was lame of me.

    >
    > Yeah, I just wasn't sure what you wanted. Like a more object-oriented
    > version or what.


    My bad.

    > Looks like abort wins the day, though.
    >
    > var = ARGV[0] or abort "Arg required"


    or Ryan's version:

    > abort "arguments are required" if ARGV.empty?


    Although actually, I'm using optparse, and it's sometimes useful to do

    usage_error += "need foo" unless @foo
    usage_error += "need bar" unless @bar
    abort usage_error unless usage_error.empty?

    --
    Giles Bowkett

    Podcast: http://hollywoodgrit.blogspot.com
    Blog: http://gilesbowkett.blogspot.com
    Portfolio: http://www.gilesgoatboy.org
    Tumblelog: http://giles.tumblr.com
     
    Giles Bowkett, Dec 4, 2007
    #15
  16. Giles Bowkett

    botp Guest

    On Dec 5, 2007 6:01 AM, Giles Bowkett <> wrote:
    > Although actually, I'm using optparse, and it's sometimes useful to do
    > usage_error += "need foo" unless @foo
    > usage_error += "need bar" unless @bar
    > abort usage_error unless usage_error.empty?


    Ah, may i suggest Ara's main gem ...

    C:\family\ruby>cat test.rb
    require 'rubygems'
    require 'main'

    Main {
    argument 'foo'
    argument 'bar'

    def run
    p params['foo']
    p params['bar']
    end
    }

    C:\family\ruby>ruby test.rb
    argument(foo) not given

    C:\family\ruby>ruby test.rb foo
    argument(bar) not given

    C:\family\ruby>ruby test.rb foo bar
    #<Main::parameter::Argument:0x2b59bfc @cast=nil, @arity=1,
    @values=["foo"], @names=["foo"], @defaults=[], @required=true,
    @validate=nil, @given=true, @type=:argument>
    #<Main::parameter::Argument:0x2b598dc @cast=nil, @arity=1,
    @values=["bar"], @names=["bar"], @defaults=[], @required=true,
    @validate=nil, @given=true, @type=:argument>


    C:\family\ruby>ruby test.rb -h
    NAME
    test.rb

    SYNOPSIS
    test.rb foo bar [options]+

    PARAMETERS
    foo (1 -> foo)
    bar (1 -> bar)
    --help, -h


    kind regards -botp
     
    botp, Dec 5, 2007
    #16
    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. David Corbin
    Replies:
    3
    Views:
    113
    Robert Klemme
    Oct 13, 2003
  2. zuzu
    Replies:
    7
    Views:
    259
    Aredridel
    Jul 19, 2004
  3. stevetuckner

    DRY ruby idiom

    stevetuckner, Sep 28, 2004, in forum: Ruby
    Replies:
    11
    Views:
    217
    Brian Candler
    Sep 29, 2004
  4. Corey
    Replies:
    18
    Views:
    248
    Dave Thomas
    Nov 12, 2004
  5. Peña, Botp
    Replies:
    7
    Views:
    424
    Florian Gross
    Mar 21, 2005
Loading...

Share This Page