Your Favorite One Liner

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"

···

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

···

On Wed, 1 Mar 2006, Daniel Nugent wrote:

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"

--
-Dan Nugent

--
judge your success by what you had to give up in order to get it.
- h.h. the 14th dali lama

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

Isn't that cool? :wink:

James Edward Gray II

···

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

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

···

On 3/1/06, Daniel Nugent <nugend@gmail.com> wrote:

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"

--
-Dan Nugent

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

James Edward Gray II wrote:

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

···

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

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

···

On 3/1/06, Farrel Lifson <farrel.lifson@gmail.com> wrote:

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

On 3/1/06, Daniel Nugent <nugend@gmail.com> wrote:
> 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"
>
> --
> -Dan Nugent
>
>

William James wrote:

James Edward Gray II wrote:
>
> > 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"]

···

> On Feb 28, 2006, at 7:59 PM, Daniel Nugent wrote:

Farrel Lifson wrote:

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.

William James wrote:

Farrel Lifson wrote:

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.

mv primes.rb non_composite_natural_numbers.rb

Jeffrey Schwab <jeff@schwabcenter.com> writes:

William James wrote:

Farrel Lifson wrote:

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.

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