Your Favorite One Liner

D

Daniel Nugent

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"
 
A

ara.t.howard

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"


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
 
J

James Edward Gray II

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

This was originally posted by Erik Terpstra, with some editing from
others to improve it:
=> [["This is a"], ["test of"], ["the"], ["emergency"],
["broadcasting"], ["services"]]

Isn't that cool? ;)

James Edward Gray II
 
W

William James

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

This was originally posted by Erik Terpstra, with some editing from
others to improve it:
=> [["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"]]
 
W

William James

William said:
James said:
Give out your favorite one liner, what it does, and when you use it.

This was originally posted by Erik Terpstra, 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"]]

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"]]

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"]
 
F

Farrel Lifson

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

Farrel Lifson

Whoops here it is....

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

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



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"
 
W

William James

Farrel said:
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

1 isn't a prime number.
 
H

horndude77

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]' <num>

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

-----Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top