[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Giles Bowkett

12/4/2007 6:42:00 AM

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.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

15 Answers

Justin Collins

12/4/2007 7:07:00 AM

0

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

MonkeeSage

12/4/2007 7:18:00 AM

0

On Dec 4, 12:41 am, Giles Bowkett <gil...@gmail.com> 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.bl...
> Blog:http://gilesbowkett.bl...
> Portfolio:http://www.gilesg...
> Tumblelog:http://giles....

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

Justin Collins

12/4/2007 7:26:00 AM

0

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"



Ryan Davis

12/4/2007 9:47:00 AM

0


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


Giles Bowkett

12/4/2007 4:31:00 PM

0

> > 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.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Giles Bowkett

12/4/2007 4:32:00 PM

0

> > 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.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

Giles Bowkett

12/4/2007 4:33:00 PM

0

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

--
Giles Bowkett

Podcast: http://hollywoodgrit.bl...
Blog: http://gilesbowkett.bl...
Portfolio: http://www.gilesg...
Tumblelog: http://giles....

MonkeeSage

12/4/2007 5:02:00 PM

0

On Dec 4, 10:31 am, Giles Bowkett <gil...@gmail.com> 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.bl...
> Blog:http://gilesbowkett.bl...
> Portfolio:http://www.gilesg...
> Tumblelog:http://giles....

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

Tim Hunter

12/4/2007 5:20:00 PM

0

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

James Gray

12/4/2007 5:30:00 PM

0

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