[Vit-discuss] Top Ruby Projects algorithm?
James Edward Gray II
james at grayproductions.net
Fri Aug 25 23:32:18 EDT 2006
On Aug 25, 2006, at 10:16 PM, John W. Long wrote:
> A better non-oo Hello World?
I've already told John, but I'm partial to the current hello world
with point by point comments. ;)
> One using blocks:
>
> # Ruby can do amazing
> # things with blocks:
> 5.times do
> puts "Ruby rocks!"
> end
>
> # A complex example:
> a = [1, 2, 3]
> total = a.inject(0) do
> |sum, i|
> sum + i
> end
> puts total #=> 6
I like this one.
I wrote a fibonacci snippet a while back, but VIT Core thought it was
a bit complex:
fib = Hash.new do |seq, i|
if i < 2
seq[i] = i
else
seq[i] = seq[i - 2] + seq[i - 1]
end
end
0.upto(100) do |n|
puts fib[n]
end
James Edward Gray II
More information about the vit-discuss
mailing list