Forum: Ruby Your Favorite One Liner

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel N. (Guest)
on 2006-03-01 04:17
(Received via mailing list)
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"
unknown (Guest)
on 2006-03-01 04:35
(Received via mailing list)
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
James G. (Guest)
on 2006-03-01 06:42
(Received via mailing list)
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?  ;)

James Edward G. II
William J. (Guest)
on 2006-03-01 10:28
(Received via mailing list)
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"]]
William J. (Guest)
on 2006-03-01 11:28
(Received via mailing list)
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"]
Farrel L. (Guest)
on 2006-03-01 22:04
(Received via mailing list)
I did this while messing around at work one afternoon. Pretty sure it
could be whittled down and made more efficient.
Farrel L. (Guest)
on 2006-03-01 22:04
(Received via mailing list)
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
William J. (Guest)
on 2006-03-01 23:23
(Received via mailing list)
Farrel L. wrote:
> 11
> 13
> 17
> 19
> 23
> 29

1 isn't a prime number.
Jeffrey S. (Guest)
on 2006-03-02 00:34
(Received via mailing list)
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
Jay A. (Guest)
on 2006-03-02 03:28
(Received via mailing list)
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
Steven L. (Guest)
on 2006-03-02 20:07
(Received via mailing list)
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
This topic is locked and can not be replied to.