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 --noverbosemight be suitable (.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=>)