[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Your favorite bit of ruby code?

Carl Lerche

2/8/2007 8:49:00 PM

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)

31 Answers

WoNáDo

2/8/2007 9:04:00 PM

0

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

John Carter

2/8/2007 9:36:00 PM

0

John Carter

2/8/2007 9:46:00 PM

0

John Carter

2/8/2007 9:54:00 PM

0

M. Edward (Ed) Borasky

2/9/2007 3:41:00 AM

0

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

If God had meant for carrots to be eaten cooked, He would have given rabbits fire.


ramalho@gmail.com

2/9/2007 6:05:00 AM

0

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
> def factorial
> return 1 if self <= 1
> self * (self-1).factorial
> 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
>
>
>

ramalho@gmail.com

2/9/2007 6:18:00 AM

0

John's snipped is brilliant. I just thought the last line could be like this:

######

# Ruby is Objects all the way down and open for extension...
class Integer
def factorial
return 1 if self <= 1
self * (self-1).factorial
end
end

puts 1000.factorial

######

Using puts makes it work outside of irb, and 1000.factorial shows off Bignum.

It's a pity the factorial method can't be named just "!"...

Cheers,

Luciano




On 2/9/07, Luciano Ramalho <ramalho@gmail.com> wrote:
> On 2/8/07, John Carter <john.carter@tait.co.nz> wrote:
> > # Ruby is Objects all the way down and open for extension...
> >
> > class Integer
> > def factorial
> > return 1 if self <= 1
> > self * (self-1).factorial
> > 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
> >
> >
> >
>

Trans

2/9/2007 7:42:00 AM

0


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!

class Functor < Proc
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding
$)/ }

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

def method_missing(op, *args, &blk)
call(op, *args, &blk)
end
end

usage example:

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

T.


Harold Hausman

2/9/2007 7:59:00 AM

0

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

# I clearly remember this thing making me smile as I wrote it
# of course, it's imperfect, as is everything.
class LSystem
attr_reader :output

def initialize( in_axiom, in_rules, in_iterations = 0 )
@axiom = in_axiom
@output = in_axiom
@rules = in_rules

in_iterations.times do iterate end

return @output
end

def iterate
temp_string = ""
@output.scan( /./ ) do |letter|
rule_hit = false
@rules.each do |rule|
if( letter[ rule[0] ] )
rule_hit = true
temp_string << rule[1]
end
end
if( not rule_hit )
temp_string << letter
end
end
@output = temp_string
end
end

## Example usage:
require 'LSystem'

the_rules = [
[ /F/, "" ],
[ /Y/, "+FX--FY+" ],
[ /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

Pit Capitain

2/9/2007 8:27:00 AM

0

Harold Hausman schrieb:
> On 2/9/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!"
>
> (... code sample ...)
>
> ## Of course, the output isn't very useful without a turtle graphics
> system. ;)

Harold, the output might not be useful, but at least it's interesting.
Do you have a picture of what it would look like?

Regards,
Pit