
Ruby One-Liners - kawera
https://benoithamelin.tumblr.com/ruby1line/
======
kbenson
I knew Ruby copied a lot from Perl, but I wasn't prepared for just how much
these are exactly how you would do the same thing in perl, just with the the
order of passing lists reversed.

The command line switches are the same, the BEGIN/END block stuff is the same,
the $F auto-splitting is the same.

Good on Ruby, that's the stuff that makes Perl one-liners actually worth
coming back to again and again.

~~~
faitswulff
What is that BEGIN{...} and END{...} syntax? I've seen a good amount of Ruby,
but haven't encountered that yet.

~~~
jhbadger
It's basically there to make Ruby an equivalent to AWK. It only makes sense if
you are looping over the lines with -n. The BEGIN block runs at the start of
the run, and the END block at the end. They are useful for things like
initializing a sum variable to zero at the start and printing it out at the
end.

------
par
What about getting some Obfuscated Ruby one-liners, like the well known Perl
Obfuscation Contests?
[https://www.foo.be/docs/tpj/issues/vol4_3/tpj0403-0017.html](https://www.foo.be/docs/tpj/issues/vol4_3/tpj0403-0017.html)
:)

~~~
creatonez
I think the natural successor to IOCCC and the Obfuscated Perl Contest is the
Code Golf^1 community on stack exchange

^1:
[https://codegolf.stackexchange.com/questions?tab=Votes](https://codegolf.stackexchange.com/questions?tab=Votes)

------
stevebmark
If you're going to write unreadable code why not use Perl? It's much faster
than Ruby. Or Awk, even though it's one of the worst programming languages
ever created?

~~~
aexl
Why is awk one of the "worst programming languages ever created"?

------
asicsp
I made a compilation of Ruby one-liners [1] as well last year.

There's plenty of stuff that is similar to Perl, which in turn had borrowed
from awk, sed and other tools. The biggest difference I'd pick is Perl's
context based operation.

    
    
        # Ruby requires explicit conversion with to_i
        ruby -ane 'print if $F[1].to_i > 35'
        perl -ane 'print if $F[1] > 35'
    

Performance is slower compared to Perl, but to those who already know Ruby,
this would suit better than learning the nuances of another language.

[1] [https://github.com/learnbyexample/Command-line-text-
processi...](https://github.com/learnbyexample/Command-line-text-
processing/blob/master/ruby_one_liners.md)

~~~
pizza234
There are other significant other functionalities that I find very handy in
oneliners, notably:

\- automatic associative array instantiation

\- the return value of regular expressions with a capture group is the value
matched by it (in Ruby, the closest to this is `[/regex/, <group_i>]`)

------
cgranier
I always liked this one I wrote ages ago, to generate strong (at the time)
passwords:

[code lang=“ruby”]

((33..126).map { |i| i.chr }).to_a.shuffle[0..14].join

[/code]

~~~
Axsuul
A bit more efficient:

14.times.map { rand(33..126).chr }.shuffle.join

~~~
e12e
Why shuffle? Does it do something rand doesn't already do?

At any rate, would probably want to use [https://ruby-
doc.org/stdlib-2.5.1/libdoc/securerandom/rdoc/S...](https://ruby-
doc.org/stdlib-2.5.1/libdoc/securerandom/rdoc/Securerandom.html) for password
generation.

Also, I think you might just want an alphabet and use sample: [https://ruby-
doc.org/core-2.5.1/Array.html#method-i-sample](https://ruby-
doc.org/core-2.5.1/Array.html#method-i-sample)

# unsure if you need a:

require 'securerandom'

(33..126).sample(14, random: SecureRandom).map(&:chr).join

~~~
Axsuul
You're right, probably doesn't need shuffle :)

------
bdcravens
OT: on mobile the authors bio fills up the entire screen. The headline is
beneath the fold.

------
empath75

      1.upto(100){|n|puts'FizzBuzz'[i=n**4%-15,i+13]||n}

------
eyegor
Maybe I'm being pedantic, but half of these use ";" to separate statements so
I wouldn't really call them one liners.

~~~
kbenson
A one liner does not mean "one line of source code in a file", it means "a
single shell statement". That's why the Ruby command is prefixing them all. I
routinely write 10-20 statement Perl one-liners at work for one off things.

For example, just yesterday it was to parse a log, grep for specific lines,
pull out a time and a number per line, display name, number, and some number
of characters afterwards as a quick abnd dirty graph of the magnitude to make
it obvious if there were major outliers and to try to see trends over time.
Still a one-liner.

~~~
bdcravens
In theory, couldn’t you then compile a Rails app into a one-liner? (Seems the
max line length varies, but is at least hundreds of thousands of characters in
most OSes)

~~~
kbenson
You could, but it would be stupid to do so if it wasn't a trivial app, since
nobody wants to support something like that, even if it's lifetime is measured
in minutes or hours. It's a one-liner, just a pointless one.

That said, if rails has a super simple default startup that could serve a
file, then it might be useful to start that as a one liner in select
circumstances.

~~~
vinceguidry
Rails has generators for that. Invaluable for greenfielding projects in an
iterative manner. Blow everything away and recreate with a single command.

[https://guides.rubyonrails.org/generators.html](https://guides.rubyonrails.org/generators.html)

------
edmoffo
This is what I love Ruby for: one-liners! :) Thanks for the summary, truly
helpful.

------
draw_down
I would never be able keep straight the escaping for these incantations.
Writing code at the shell is not fun.

~~~
tyingq
Not really an issue unless the one liner itself has single quotes in it, or
perhaps you're using Windows?

------
j88439h84
I like to build lots of cool command-line one-liners in Python with the Mario
tool.

[https://github.com/python-mario/mario](https://github.com/python-mario/mario)

