[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Your Favorite One Liner

Daniel Nugent

3/1/2006 1:59:00 AM

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


9 Answers

Ara.T.Howard

3/1/2006 2:36:00 AM

0

James Gray

3/1/2006 4:43:00 AM

0

On Feb 28, 2006, at 7:59 PM, Daniel Nugent 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"]]

Isn't that cool? ;)

James Edward Gray II


William James

3/1/2006 8:23:00 AM

0

James Edward Gray II wrote:
> On Feb 28, 2006, at 7:59 PM, Daniel Nugent 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"]]

William James

3/1/2006 9:26:00 AM

0

William James wrote:
> James Edward Gray II wrote:
> > On Feb 28, 2006, at 7:59 PM, Daniel Nugent 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"]

Farrel Lifson

3/1/2006 8:04:00 PM

0

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


Farrel Lifson

3/1/2006 8:04:00 PM

0

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

3/1/2006 9:23:00 PM

0


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.

Jeff Schwab

3/1/2006 10:06:00 PM

0

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

horndude77

3/2/2006 1:27:00 AM

0

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