Reputation: 840
I want to write script in ruby. I would like that script will return true or false. Is it possible? Can I return value from ruby script?
Upvotes: 0
Views: 530
Reputation: 888
Run a ruby script, it will return a true/false value to tell command if it run success or fail.
At the end of you rb file, add a judge code to tell command if rb file run success:
if @success
return true
else
raise "RuntimeError"
end
Cover @success
as you condition, when error occure, run script will return false, eles true
Upvotes: 0
Reputation: 13673
Yes! Kernel.exit
takes an optional argument. Put this into exit.rb
:
#!/usr/bin/env ruby
puts "Hello, world"
exit 1
Verify like so:
$ ruby exit.rb ; echo $?
Hello, world
1
Upvotes: 2
Reputation: 8821
Yes, you can. For ruby, the value of the last expression is the default return value.
For example:
2.1.2 :001 > def test
2.1.2 :002?> true
2.1.2 :003?> end
=> :test
2.1.2 :004 > test
=> true
2.1.2 :005 > def test1
2.1.2 :006?> false
2.1.2 :007?> end
=> :test1
2.1.2 :008 > test1
=> false
2.1.2 :009 > def test2
2.1.2 :010?> 100
2.1.2 :011?> end
=> :test2
2.1.2 :012 > test2
=> 100
Upvotes: 0