Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing boolean parameter in ruby

Tags:

ruby

I am wondering how do I pass a false value to my ruby script.

If I invoke:

ruby myscript.rb false

and then in my script if I say:

my_class.new(*ARGV)
or my_class.new(ARGV[0])

basically a string with value "false" gets passed. Clearly if I say

if(ARGV[0]){ do something} .. this gets executed even if value passed is false. 

Can I change my function signature to auto-covert paramter to boolean ..so that I dont have to do

if(ARGV[0]=='true')
like image 541
codeObserver Avatar asked Feb 10 '12 21:02

codeObserver


Video Answer


1 Answers

You need to evaluate the command-line argument. Anything passed on the command line is a string, that's all the command line knows about.

For example, you could monkey-patch String (untested):

class String
  def to_b
    self =~ /^(true|t|yes|y|1)$/i
  end
end

Or write a utility, or use the command-line option parser (long-term best bet), or...

like image 125
Dave Newton Avatar answered Sep 25 '22 10:09

Dave Newton