backtick vs system in ruby - ruby

Base on all my readings on the web the difference between backtick and system is what is returned . backtick returns STDOUT while system returns true or false.
And I was told that both of them use subshell to perform the operation.
However I am noticing another difference.
output = system('aaa')
puts "output is: #{output}"
output = `aaa`
puts "output is: #{output}"
Result of above code is
$ ruby test.rb
output is:
lab.rb:4:in ``': No such file or directory - aaa (Errno::ENOENT) from test.rb:4:in `<main>'
So it seems in the case of backtick the exceptions are raised to the main program. Operation system swallows the exception and the main program never sees the exception.
I'm using ruby 1.9.3 .
Is my analysis right ?
UPDATE: Got the answer. It's here https://gist.github.com/3730986 .

Copying the answer from the edited question body in order to remove this question from the "Unanswered" filter:
Got the answer. It's here https://gist.github.com/3730986 .
~ answer per nodejs99

Related

How to execute a Ruby code from inside a Ruby script?

I'm trying to execute a Ruby script file.
Assuming the input is a string that contains the file content.
What are the possible ways? taking into considerations that I need to keep the output of the executed file whether stdout or not separated from the Main script.
As an example of what I'm trying to do is have a function called execute(code)
Then calling execute('4 + 5') would return 9 although I can write a whole Ruby script in the place of '4 + 5'.
If anyone can forward me to any related tutorials or books, I'd be thankful :)
You can call shell commands in Ruby, it's as simple and intuitive as surrounding your desired command in backticks.
The output gets returned, so just save it to a variable:
script1.rb:
puts "asdf"
script2.rb:
output = `ruby script1.rb`
puts output
"asdf"
I question what exactly it is you're trying to do, though. Because this is totally unintuitive and roundabout. Are you sure you aren't just looking for a module or something?

Why might interactive ruby stop showing results?

I was just playing around with interactive ruby.
Near the beginning (line 138), I did
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
And that above worked
Then I tried a bunch of lines 139-147 experimenting to get more used to the language
Then I wasn't getting results and I tried some even simpler things I expected would work, 148-154 and didn't get any result.
So it looks like at some point one of my commands might've stopped it from displaying results though i'm not sure what.
I'd like to get it to display the results again. I suppose I could try to exit and go back in but i'd rather a way without doing that,
. irb(main):138:0> ['rock','paper','scissors'].index('paper')
=> 1
irb(main):139:0> a=[1,2,3
irb(main):140:1> a
irb(main):141:1> a=[1.2.3]
irb(main):142:1> a[0]
irb(main):143:1> a(0)
irb(main):144:1> a=[1,2,3]
irb(main):145:1> a(1)
irb(main):146:1> puts a(1)
irb(main):147:1> puts a[1]
irb(main):148:1> a
irb(main):149:1> a=[1,2,3]
irb(main):150:1> a
irb(main):151:1> h={4=>4}
irb(main):152:1> h
irb(main):153:1> puts 6
irb(main):154:1>
If it makes any difference this is my version number and OS is Windows.
C:\blah>ruby -v
ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
C:\blah>
Because of this line here:
irb(main):139:0> a=[1,2,3
You haven't closed off the array with a closing ]. the :1 in irb(main):154:1> makes it clear you're inside a nested expression.
If you enter another ], you'll get a big syntax error because all of what you've entered before it isn't valid array syntax, but then you can move on.
Notice that since late 2019, assignment does not output any more the value in irb.
This is a new case where Interactive Ruby (irb) stop showing result.
NB : you can get the old behavior by setting IRB.conf[:ECHO_ON_ASSIGNMENT] = true in your ~/.irbrc.

Ruby: Why do I get warning "regex literal in condition" here?

A simple Ruby program, which works well (using Ruby 2.0.0):
#!/usr/bin/ruby
while gets
print if /foo/../bar/
end
However, Ruby also outputs the warning warning: regex literal in condition. It seems that Ruby considers my flip-flop-expression /foo/../bar/ as dangerous.
My question: Where lies the danger in this program? And: Can I turn off this warning (ideally only for this statement, keeping other warnings active)?
BTW, I found on the net several discussions of this kind of code, also mentioning the warning, but never found a good explanation why we get warned.
You can avoid the warning by using an explicit match:
while gets
print if ~/foo/..~/bar/
end
Regexp#~ matches against $_.
I don't know why the warning is shown (to be honest, I wasn't even aware that Ruby matches regexp literals against $_ implicitly), but according to the parser's source code, it is shown unless you provide the -e option when invoking Ruby, i.e. passing the script as an argument:
$ ruby -e "while gets; print if /foo/../bar/ end"
I would avoid using $_ as an implicit parameter and instead use something like:
while line = gets
print line if line=~/foo/..line=~/bar/
end
I think Neil Slater is right: It looks like a bug in a parser. If I change the code to
#!/usr/bin/ruby
while gets
print if $_=~/foo/..$_=~/bar/
end
the warning disappears.
I'll file a bug report.

How can I comment out my Ruby return values with something like "# =>"?

Having just started with Ruby and while following a tutorial, the result was shown as:
a + b # => 3
I have never seen such a possibility; that seems so handy! Could you please tell me what it is? is it proprietary or is it for everyone?
Josh Cheek's seeing is believing. Apparently you can run it over your code, or it can be integrated in several editors.
Reconfigure Your REPL
The # symbol is a comment in Ruby. By default, most Ruby REPLs (e.g. irb or pry) will use => to prefix the return value of your last expression.
In IRB, you can modify this prefix so that each return value is prefixed by a different string. You can do this through the IRB::Context#return_format method on your conf instance. For example:
$ irb
irb(main):001:0> conf.return_format = "#=> %s\n"
#=> "#=> %s\n"
irb(main):002:0> 1 + 2
#=> 3
More permanent changes would have to be made in your IRB configuration file by customizing the prompt through the IRB.conf[:PROMPT] Hash and then setting IRB.conf[:PROMPT_MODE] to your custom prompt, but in my opinion the solution above is simpler even though you have to run it within the current REPL session rather than saving it as a default.

Any way to automagically `puts` the last expression in a Ruby script?

I'm working on implementing Project Euler solutions as semantic Ruby one-liners. It would be extremely useful if I could coerce Ruby to automatically puts the value of the last expression. Is there a way to do this? For example:
#!/usr/bin/env ruby -Ilib -rrubygems -reuler
1.upto(100).into {|n| (n.sum.squared - n.map(&:squared).sum)
I realize I can simply puts the line, but for other reasons (I plan to eval the file in tests, to compare against the expected output) I would like to avoid an explicit puts. Also, it allots me an extra four characters for the solution. :)
Is there anything I can do?
You might try running it under irb instead of directly under a Ruby interpreter.
It seems like the options -f --noprompt --noverbose might be suitable (.
#!/usr/bin/env irb -f --noprompt --noverbose -Ilib -rrubygems -reuler
'put your one-liner here'
The options have these meanings:
-f: do not use .irbrc (or IRBRC)
--noverbose: do not display the source lines
--noprompt: do not prefix the output (e.g. with =>)
result = calculate_result
puts result if File.exist?(__FILE__)
result of eval is last executed operation just like any other code block in ruby
is doing
puts eval(file_contents)
an option for you?
EDIT
you can make use of eval's second parameter which is variables binding
try the following:
do_not_puts = true
eval(file_contents, binding)
and in the file:
....
result = final_result
if defined?(do_not_puts)
result
else
puts(result)
end
Is it an option to change the way you run scripts?
script.rb:
$_= 1.upto(100).into {|n| (n.sum.squared - n.map(&:squared).sum)
invoke with
echo nil.txt | /usr/bin/env/ruby -Ilib -rrubygems -reuler -p script.rb, where nil.txt is a file with a single newline.

Resources