# Your favorite bit of ruby code?

**URL:** <https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201>\
**Category:** ruby-talk\
**Created:** [8 February 2007 20:49 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201 "2007-02-08T20:49:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_Lerche](https://avatars.discourse-cdn.com/v4/letter/c/3da27b/32.png) [@Carl\_Lerche](https://rubytalk.org/u/Carl_Lerche)\
**Post date:** [8 February 2007 20:49 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/1 "2007-02-08T20:49:25Z")

</div>

Hello,

I'm just curious what your favorite bit of ruby code is? Do you have  
any small bits, methods, classes, or anything else, that just make you  
say "Wow, that is sweet!"

I'd like to see some of them!

thanks,  
-carl

> **···**
>
> --  
> EPA Rating: 3000 Lines of Code / Gallon (of coffee)

---

<div class="post-metadata">

**Author:** ![Wolfgang\_Nadasi-Don1](https://avatars.discourse-cdn.com/v4/letter/w/958977/32.png) [@Wolfgang\_Nadasi-Don1](https://rubytalk.org/u/Wolfgang_Nadasi-Don1)\
**Post date:** [8 February 2007 21:05 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/2 "2007-02-08T21:05:08Z")

</div>

Carl Lerche schrieb:

> I'm just curious what your favorite bit of ruby code is?

Well, in german ruby forum I use since longer time as part of my signature

def a(&a);yield(a,10);end;a{|a,i|(i==1)?(print "los gehts!\n"):(print "#{i-=1}...";a.call(a,i))}

which produces

9...8...7...6...5...4...3...2...1...los gehts!

Pretty useless.

Wolfgang Nádasi-Donner

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 February 2007 21:36 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/3 "2007-02-08T21:36:29Z")

</div>

# Ruby is Objects all the way down and open for extension...

class Integer  
&nbsp;&nbsp;&nbsp;def factorial  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1 if self \<= 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self \* (self-1).factorial  
&nbsp;&nbsp;&nbsp;end  
end

6.factorial  
720

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

> **···**
>
> On Fri, 9 Feb 2007, Carl Lerche wrote:
> 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 February 2007 21:45 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/4 "2007-02-08T21:45:49Z")

</div>

It's the IO.read().scan(%r{}mx){|m| ...} paradigm here I love...

Equally Good is `command`.scan(%r{}mx){|m| ...}

ruby -e 'IO.read("ruby/eval.c").scan(%r{^([a-z\_]+)\s\*\(.\*?\{}m){|m|puts $1}'  
rb\_jump\_context  
rb\_secure  
rb\_secure\_update  
rb\_check\_safe\_obj  
rb\_check\_safe\_str  
raise\_undef  
rb\_clear\_cache  
rb\_clear\_cache\_for\_remove  
rb\_clear\_cache\_by\_id  
rb\_clear\_cache\_by\_class

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

> **···**
>
> On Fri, 9 Feb 2007, Carl Lerche wrote:
> 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"

---

<div class="post-metadata">

**Author:** ![John\_Carter](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@John\_Carter](https://rubytalk.org/u/John_Carter)\
**Post date:** [8 February 2007 21:54 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/5 "2007-02-08T21:54:16Z")

</div>

Ok, I have run out of time to list them all in detail...

But things involving  
&nbsp;&nbsp;&nbsp;ruby -r find -e 'Find.find{|f|...}'

&nbsp;&nbsp;&nbsp;ruby -i.bak -nple '....'

&nbsp;&nbsp;&nbsp;Hash.new(0)

&nbsp;&nbsp;&nbsp;Hash.new{|hash,key| hash[key] = ....}

are very sweet

John Carter Phone : (64)(3) 358 6639  
Tait Electronics Fax : (64)(3) 359 4632  
PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
New Zealand

> **···**
>
> On Fri, 9 Feb 2007, Carl Lerche wrote:
> 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"

---

<div class="post-metadata">

**Author:** ![M\_Edward\_Ed\_Borasky1](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M\_Edward\_Ed\_Borasky1](https://rubytalk.org/u/M_Edward_Ed_Borasky1)\
**Post date:** [9 February 2007 03:41 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/6 "2007-02-09T03:41:16Z")

</div>

Carl Lerche wrote:

> Hello,
> 
> I'm just curious what your favorite bit of ruby code is? Do you have  
> any small bits, methods, classes, or anything else, that just make you  
> say "Wow, that is sweet!"
> 
> I'd like to see some of them!
> 
> thanks,  
> -carl

I've never actually looked at the code that does all the work, but my favorite bit of Ruby code from what it actually does is "Rake".

> **···**
>
> --  
> M. Edward (Ed) Borasky, FBG, AB, PTA, PGS, MS, MNLP, NST, ACMC(P)  
> [http://borasky-research.blogspot.com/](http://borasky-research.blogspot.com/)
> 
> If God had meant for carrots to be eaten cooked, He would have given rabbits fire.

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [9 February 2007 07:41 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/7 "2007-02-09T07:41:46Z")

</div>

class Functor \< Proc  
&nbsp;&nbsp;&nbsp;&nbsp;private \*instance\_methods.select { |m| m !~ /(^\_\_|^\W|^binding  
$)/ }

&nbsp;&nbsp;&nbsp;&nbsp;def initialize(&function)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super(&function)  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;def method\_missing(op, \*args, &blk)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;call(op, \*args, &blk)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

usage example:

&nbsp;&nbsp;f = Functor.new { |op, x| x.send(op, x) }  
&nbsp;&nbsp;f + 1 #=\> 2  
&nbsp;&nbsp;f + 2 #=\> 4  
&nbsp;&nbsp;f + 3 #=\> 6  
&nbsp;&nbsp;f \* 1 #=\> 1  
&nbsp;&nbsp;f \* 2 #=\> 2  
&nbsp;&nbsp;f \* 3 #=\> 9

T.

> **···**
>
> On Feb 8, 3:49 pm, "Carl Lerche" \<carl.ler...@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"
> > 
> > I'd like to see some of them!

---

<div class="post-metadata">

**Author:** ![Harold\_Hausman](https://avatars.discourse-cdn.com/v4/letter/h/ac91a4/32.png) [@Harold\_Hausman](https://rubytalk.org/u/Harold_Hausman)\
**Post date:** [9 February 2007 07:59 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/8 "2007-02-09T07:59:20Z")

</div>

# I clearly remember this thing making me smile as I wrote it  
# of course, it's imperfect, as is everything.  
class LSystem  
&nbsp;&nbsp;attr\_reader :output

&nbsp;&nbsp;def initialize( in\_axiom, in\_rules, in\_iterations = 0 )  
&nbsp;&nbsp;&nbsp;&nbsp;@axiom = in\_axiom  
&nbsp;&nbsp;&nbsp;&nbsp;@output = in\_axiom  
&nbsp;&nbsp;&nbsp;&nbsp;@rules = in\_rules  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;in\_iterations.times do iterate end

&nbsp;&nbsp;&nbsp;&nbsp;return @output  
&nbsp;&nbsp;end  
&nbsp;&nbsp;  
&nbsp;&nbsp;def iterate  
&nbsp;&nbsp;&nbsp;&nbsp;temp\_string = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@output.scan( /./ ) do |letter|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rule\_hit = false  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@rules.each do |rule|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( letter[rule[0] ] )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rule\_hit = true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp\_string \<\< rule[1]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if( not rule\_hit )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;temp\_string \<\< letter  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;@output = temp\_string  
&nbsp;&nbsp;end  
end

## Example usage:  
require 'LSystem'

the\_rules = [  
&nbsp;&nbsp;[/F/, ""],  
&nbsp;&nbsp;[/Y/, "+FX--FY+"],  
&nbsp;&nbsp;[/X/, "-FX++FY-"]  
]

the\_system = LSystem.new( "FX", the\_rules, 10 )

p the\_system.output

## Of course, the output isn't very useful without a turtle graphics system. 😉

Regards,  
-Harold

> **···**
>
> On 2/9/07, Carl Lerche \<carl.lerche@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"
> > 
> > I'd like to see some of them!

---

<div class="post-metadata">

**Author:** ![Julian\_Julik\_Tarkhan](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@Julian\_Julik\_Tarkhan](https://rubytalk.org/u/Julian_Julik_Tarkhan)\
**Post date:** [9 February 2007 08:41 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/9 "2007-02-09T08:41:55Z")

</div>

within\_transaction do | cursor |  
&nbsp;&nbsp;&nbsp;&nbsp;raise "We have a problem" unless cursor.connected?  
&nbsp;&nbsp;&nbsp;&nbsp;@result = db.find(id) || db.find(DEFAULT\_RECORD)  
end

> **···**
>
> On Feb 8, 2007, at 9:49 PM, Carl Lerche wrote:
> 
> > I'd like to see some of them!
> 
> --  
> Julian 'Julik' Tarkhanov  
> please send all personal mail to  
> me at julik.nl

---

<div class="post-metadata">

**Author:** ![S.Volkov](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@S.Volkov](https://rubytalk.org/u/S.Volkov)\
**Post date:** [9 February 2007 09:20 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/10 "2007-02-09T09:20:08Z")

</div>

"Carl Lerche" \<carl.lerche@gmail.com\> wrote in message  
news:ab90312e0702081249ib5fcd61y924299d020f48526@mail.gmail.com...

> I'd like to see some of them!

my 2c:

> **···**
>
> ###  
> Fibonacci = Hash.new{ |ht, k| ht[k] = k\<2 ? 1 : ht[k-1] + ht[k-2] }  
> # Sample  
> p Fibonacci[100]  
> #-\> 573147844013817084101
> 
> ###  
> class String  
> &nbsp;&nbsp;&nbsp;&nbsp;# define new String method for 'array' springs replacement  
> &nbsp;&nbsp;&nbsp;&nbsp;def gsubs\_a originals, replacements  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# create replacements table  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orig2repl = Hash[\*originals.zip( replacements ).flatten]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# regexp matching any original  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regexp = /#{originals.map{ |s| Regexp.escape s }.join( '|' )}/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# substitute each original with replacement from table  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.gsub( regexp ){ |orig| orig2repl[orig] }  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> end  
> # Sample  
> puts "AB[^1-2$].XY".gsubs\_a( ["AB", "1-2", "XY", "."], ["CC", "99", "ZZ",  
> "+"] )  
> #-\> CC[^99$]+ZZ
> 
> enjoy!  
> Sergey

---

<div class="post-metadata">

**Author:** ![Forum](https://avatars.discourse-cdn.com/v4/letter/f/43a26b/32.png) [@Forum](https://rubytalk.org/u/Forum)\
**Post date:** [9 February 2007 10:34 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/11 "2007-02-09T10:34:09Z")

</div>

Without hesitation my favorite is [Ruby Quiz - metakoans.rb (#67)](http://rubyquiz.com/quiz67.html)  
and some of the solutions.  
Ara's king 😉

> **···**
>
> On 2/8/07, Carl Lerche \<carl.lerche@gmail.com\> wrote:
> 
> > Hello,
> > 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"
> > 
> > I'd like to see some of them!
> > 
> > thanks,  
> > -carl
> 
> --
> 
> > EPA Rating: 3000 Lines of Code / Gallon (of coffee)
> 
> --  
> We have not succeeded in answering all of our questions.  
> In fact, in some ways, we are more confused than ever.  
> But we feel we are confused on a higher level and about more important  
> things.  
> -Anonymous

---

<div class="post-metadata">

**Author:** ![Drew\_Olson](https://avatars.discourse-cdn.com/v4/letter/d/c67d28/32.png) [@Drew\_Olson](https://rubytalk.org/u/Drew_Olson)\
**Post date:** [9 February 2007 18:11 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/12 "2007-02-09T18:11:11Z")

</div>

Carl Lerche wrote:

> I'm just curious what your favorite bit of ruby code is? Do you have

The codegolf line for printing the first 34 lines of pascal's triangle:

34.times{k=0;puts ($\*.map!{|i|k+k=i}\<\<1)\*" "}

Man, that's succinct.

-Drew

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [9 February 2007 18:22 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/13 "2007-02-09T18:22:46Z")

</div>

Hi --

> Hello,
> 
> I'm just curious what your favorite bit of ruby code is? Do you have  
> any small bits, methods, classes, or anything else, that just make you  
> say "Wow, that is sweet!"
> 
> I'd like to see some of them!

I love tons of it but one idiom I've always thought was particularly  
striking and expressive is the entry into an object's singleton class:

&nbsp;&nbsp;&nbsp;class \<\< obj

I love the idea that "\<\< obj" (which I process as something like "from  
this object" or "of this object") is enough to satisfy the class  
keyword.

David

> **···**
>
> On Fri, 9 Feb 2007, Carl Lerche wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Marcello\_Barnaba](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Marcello\_Barnaba](https://rubytalk.org/u/Marcello_Barnaba)\
**Post date:** [9 February 2007 23:10 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/14 "2007-02-09T23:10:52Z")

</div>

Hi,

> Hello,
> 
> I'm just curious what your favorite bit of ruby code is? Do you have  
> any small bits, methods, classes, or anything else, that just make you  
> say "Wow, that is sweet!"

> I'd like to see some of them!

require 'irb'

module IRB  
&nbsp;&nbsp;def IRB.start\_in\_binding(b)  
&nbsp;&nbsp;&nbsp;&nbsp;setup(nil)  
&nbsp;&nbsp;&nbsp;&nbsp;irb = IRB::Irb.new(WorkSpace.new(b))  
&nbsp;&nbsp;&nbsp;&nbsp;@CONF[:MAIN\_CONTEXT] = irb.context  
&nbsp;&nbsp;&nbsp;&nbsp;catch(:IRB\_EXIT) { irb.eval\_input }  
&nbsp;&nbsp;end  
end

(taken from [http://rubyurl.com/K4P\](http://rubyurl.com/K4P%5C))

> **···**
>
> On Thursday 08 February 2007 21:49, Carl Lerche wrote:  
> --  
> pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [10 February 2007 01:09 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/15 "2007-02-10T01:09:46Z")

</div>

I can't remember where I first saw this bit of code but I was able to  
google and find a ruby-talk posting by Wayne Vucenic with:

def quicksort(a)  
&nbsp;&nbsp;&nbsp;return a if a.size \<= 1  
&nbsp;&nbsp;&nbsp;pivot = a[0]  
&nbsp;&nbsp;&nbsp;quicksort(a.select {|i| i \< pivot }) +  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a.select {|i| i == pivot } +  
&nbsp;&nbsp;&nbsp;quicksort(a.select {|i| i \> pivot })  
end

I realize that Ruby has a built in sort but I was absolutely stunned  
at how well the quicksort algorithm could be coded in Ruby with  
little if any extraneous syntax.

Here is another version that might even be 'better' (via Gabriele Renzi)

def qs(a)  
&nbsp;&nbsp;&nbsp;&nbsp;return a if a.size \<=1  
&nbsp;&nbsp;&nbsp;&nbsp;pivot = a.shift  
&nbsp;&nbsp;&nbsp;&nbsp;less, more = a.partition{|y| y \< pivot}  
&nbsp;&nbsp;&nbsp;&nbsp;qs(less) + [pivot] + qs(more)  
end

In ruby 1.9, splat is a little more flexible, which gives:

def qs(a)  
&nbsp;&nbsp;&nbsp;&nbsp;return a if a.size \<=1  
&nbsp;&nbsp;&nbsp;&nbsp;pivot = a.shift  
&nbsp;&nbsp;&nbsp;&nbsp;less, more = a.partition{|y| y \< pivot}  
&nbsp;&nbsp;&nbsp;&nbsp;[\*qs(less), pivot, \*qs(more)] # only works in 1.9  
end

And if you want to use the nice open class features of Ruby:

class Array  
&nbsp;&nbsp;&nbsp;def qs  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return self if size \<=1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pivot = shift  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less, more = partition{ |y| y \< pivot }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[\*less.qs, pivot, \*more.qs]  
&nbsp;&nbsp;&nbsp;end  
end

Gary Wright

> **···**
>
> On Feb 8, 2007, at 3:49 PM, Carl Lerche wrote:
> 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"

---

<div class="post-metadata">

**Author:** ![Ilmari\_Heikkinen](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@Ilmari\_Heikkinen](https://rubytalk.org/u/Ilmari_Heikkinen)\
**Post date:** [10 February 2007 03:40 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/16 "2007-02-10T03:40:27Z")

</div>

This one's less "Wow" and more ha-ha-ha \</robotic laughter\>:

class String  
&nbsp;&nbsp;def to\_proc  
&nbsp;&nbsp;&nbsp;&nbsp;eval("lambda{|a|a.#{self}}")  
&nbsp;&nbsp;end  
end

[1,2,3].map &"\*200"  
# =\> [200, 400, 600]

> **···**
>
> On 2/8/07, Carl Lerche \<carl.lerche@gmail.com\> wrote:
> 
> > I'm just curious what your favorite bit of ruby code is? Do you have  
> > any small bits, methods, classes, or anything else, that just make you  
> > say "Wow, that is sweet!"

---

<div class="post-metadata">

**Author:** ![Daniel\_DeLorme](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Daniel\_DeLorme](https://rubytalk.org/u/Daniel_DeLorme)\
**Post date:** [10 February 2007 04:17 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/17 "2007-02-10T04:17:42Z")

</div>

Carl Lerche wrote:

> I'd like to see some of them!

This is one I particulary like from my personal library. It allows me to do stuff like:  
&nbsp;&nbsp;&nbsp;book.author.ergo.name  
instead of:  
&nbsp;&nbsp;&nbsp;book.author && book.author.name

require "singleton"  
class BlackHole  
&nbsp;&nbsp;&nbsp;include Singleton  
&nbsp;&nbsp;&nbsp;private  
&nbsp;&nbsp;&nbsp;def method\_missing(\*args); nil; end  
&nbsp;&nbsp;&nbsp;for m in public\_instance\_methods  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;undef\_method(m.to\_sym) unless m =~ /^\_\_.\*\_\_$/  
&nbsp;&nbsp;&nbsp;end  
end

class NilClass  
&nbsp;&nbsp;&nbsp;def ergo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BlackHole.instance  
&nbsp;&nbsp;&nbsp;end  
end

class Object  
&nbsp;&nbsp;&nbsp;def ergo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;yield(self)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [10 February 2007 05:03 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/18 "2007-02-10T05:03:35Z")

</div>

Hi,

> I'm just curious what your favorite bit of ruby code is?

I like a lot the implicit return values.

&nbsp;&nbsp;def subject str  
&nbsp;&nbsp;&nbsp;&nbsp;if str =~ /^Subject:\s\*/i then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$'  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;if (a = subject other) then ...

Bertram

> **···**
>
> Am Freitag, 09. Feb 2007, 05:49:25 +0900 schrieb Carl Lerche:
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [8 February 2007 21:45 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/19 "2007-02-08T21:45:45Z")

</div>

Can't say I've used this bit of code anywhere, but it's the same feature as  
John's post, with a twist:

class NilClass  
&nbsp;&nbsp;def to\_s  
&nbsp;&nbsp;&nbsp;&nbsp;"Hi! My name is Nil! You can call me Nil!"  
&nbsp;&nbsp;end  
end

puts nil.to\_s #=\> "Hi! My name is Nil! You can call me Nil!"

I love ruby!

Jason

> **···**
>
> On 2/8/07, John Carter \<john.carter@tait.co.nz\> wrote:
> 
> > On Fri, 9 Feb 2007, Carl Lerche wrote:
> > 
> > \>  
> > \> I'm just curious what your favorite bit of ruby code is? Do you have  
> > \> any small bits, methods, classes, or anything else, that just make you  
> > \> say "Wow, that is sweet!"
> > 
> > # Ruby is Objects all the way down and open for extension...
> > 
> > class Integer  
> > &nbsp;&nbsp;&nbsp;def factorial  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1 if self \<= 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self \* (self-1).factorial  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > 6.factorial  
> > 720
> > 
> > John Carter Phone : (64)(3) 358 6639  
> > Tait Electronics Fax : (64)(3) 359 4632  
> > PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> > New Zealand

---

<div class="post-metadata">

**Author:** ![Luciano\_Ramalho](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@Luciano\_Ramalho](https://rubytalk.org/u/Luciano_Ramalho)\
**Post date:** [9 February 2007 06:05 UTC](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201/20 "2007-02-09T06:05:19Z")

</div>

That's a very nice little demo of Ruby's charm, John.

Cheers!

Luciano

> **···**
>
> On 2/8/07, John Carter \<john.carter@tait.co.nz\> wrote:
> 
> > On Fri, 9 Feb 2007, Carl Lerche wrote:
> > 
> > \>  
> > \> I'm just curious what your favorite bit of ruby code is? Do you have  
> > \> any small bits, methods, classes, or anything else, that just make you  
> > \> say "Wow, that is sweet!"
> > 
> > # Ruby is Objects all the way down and open for extension...
> > 
> > class Integer  
> > &nbsp;&nbsp;&nbsp;def factorial  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1 if self \<= 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self \* (self-1).factorial  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > 6.factorial  
> > 720
> > 
> > John Carter Phone : (64)(3) 358 6639  
> > Tait Electronics Fax : (64)(3) 359 4632  
> > PO Box 1645 Christchurch Email : john.carter@tait.co.nz  
> > New Zealand

[Next page](https://rubytalk.org/t/your-favorite-bit-of-ruby-code/35201.md?page=2)
