Your Favorite One Liner


#1

Give out your favorite one liner, what it does, and when you use it.

For me:

puts ARGV[rand(ARGV.size)]

It randomly prints one of the command line options passed to it. I
typically use it to make a decision when I have no preference (or have
equal distaste) for my available options.

For instance

ruby -e “puts ARGV[rand(ARGVsize)]” “shoot self in head” “use Java”


#2

On Wed, 1 Mar 2006, Daniel N. wrote:

For instance

ruby -e “puts ARGV[rand(ARGVsize)]” “shoot self in head” “use Java”


-Dan Nugent

 harp:~ > cat a.rb
 hashify = lambda{ |*hashes| hashes.inject(accum={}){|accum,hash| 

accum.update hash} }

 a = {"k" => "v", "a" => "b"}
 b = {"k" => "V"}
 c = {"f" => "b"}

 p hashify[a, b, c]


 harp:~ > ruby a.rb
 {"k"=>"V", "a"=>"b", "f"=>"b"}

-a


#3

On Feb 28, 2006, at 7:59 PM, Daniel N. wrote:

Give out your favorite one liner, what it does, and when you use it.

This was originally posted by Erik T., with some editing from
others to improve it:

str = ‘This is a test of the emergency broadcasting services’
=> “This is a test of the emergency broadcasting services”

str.scan(/(.{1,10}|\S{11,})(?:\s+|$)/) # poor man’s word wrap
=> [[“This is a”], [“test of”], [“the”], [“emergency”],
[“broadcasting”], [“services”]]

Isn’t that cool? :wink:

James Edward G. II


#4

William J. wrote:

str.scan(/(.{1,10}|\S{11,})(?:\s+|$)/) # poor man’s word wrap

Furthermore,
\S{11,}
can be
\S+

str = ‘This is a test of the emergency broadcasting services here’
p str.scan(/(.{1,9}\S|\S+)(?:\s+|$)/)
—>
[[“This is a”], [“test of”], [“the”], [“emergency”], [“broadcasting”],
[“services”], [“here”]]

One character shorter and eliminates nesting of arrays:

str =
‘This is a test of the emergency broadcasting servicings I asseverate’
p str.scan(/\S.{0,8}\S(?=\s|$)|\S+/)
—>
[“This is a”, “test of”, “the”, “emergency”, “broadcasting”,
“servicings”, “I”, “asseverate”]


#5

James Edward G. II wrote:

=> [[“This is a”], [“test of”], [“the”], [“emergency”],
[“broadcasting”], [“services”]]

This doesn’t properly handle whitespace between words.

str = ‘This is a test of the emergency broadcasting services here’
p str.scan(/(.{1,10}|\S{11,})(?:\s+|$)/)
—>
[["This is a "], [“test of”], [“the”], [“emergency”], [“broadcasting”],
[“services”], [“here”]]

Furthermore,
\S{11,}
can be
\S+

str = ‘This is a test of the emergency broadcasting services here’
p str.scan(/(.{1,9}\S|\S+)(?:\s+|$)/)
—>
[[“This is a”], [“test of”], [“the”], [“emergency”], [“broadcasting”],
[“services”], [“here”]]


#6

Whoops here it is…

farrel@nicodemus ~ $ cat primes.rb
puts [1]<<(2…ARGV[0].to_i).inject([]){|p,c|p.detect{|n|c%n==0}?p:p<<c}
farrel@nicodemus ~ $ ruby primes.rb 30
1
2
3
5
7
11
13
17
19
23
29


#7

I did this while messing around at work one afternoon. Pretty sure it
could be whittled down and made more efficient.


#8

Farrel L. wrote:

11
13
17
19
23
29

1 isn’t a prime number.


#9

Since there’s a prime one-liner (sort of) here’s a fibonacci one-liner
that I quite like:

ruby -rmatrix -e ‘p (Matrix[[1,1],[1,0]]**(ARGV[0].to_i-1))[0,0]’

The cool part is that it has O(lg(n)) running time assuming the **
operator is implemented correctly.

-----Jay


#10

William J. wrote:

5
7
11
13
17
19
23
29

1 isn’t a prime number.

mv primes.rb non_composite_natural_numbers.rb


#11

Jeffrey S. removed_email_address@domain.invalid writes:

3
mv primes.rb non_composite_natural_numbers.rb
Or just remove “[1]<<”. The above actually builds an array like:

[1, [2, 3, 5,…]]

which probably isn’t what you want anyway.

Steve