[Vit-discuss] Top Ruby Projects algorithm?

Martin DeMello martindemello at gmail.com
Fri Aug 25 16:20:11 EDT 2006


On 8/26/06, Todd Willey <xtoddx at gmail.com> wrote:
> On 8/25/06, Martin DeMello <martindemello at gmail.com> wrote:
> >
> > a = 1
> > 2.upto(1000) {|i| a = a * i}
> > puts a
>
> why not:
> puts (2...1000).inject(1){|fact,current| fact * current}

Because the snippet is intended to show off

1: The "reads-like-English" method a.upto(b)
2: The fact that ruby transparently handles Bignums

"inject" is somewhat obscure; most newcomers won't take the time to
puzzle out the snippet but simply dismiss it as some weird language
construct.

In general, the snippets should focus on neat ruby features that are
generally unavailable in other languages, but which are readable
without having to be familiar with the language.

Array difference might be another good candidate:

cities = %w(London Oslo Paris Amsterdam Berlin)
visited = %w(Berlin Oslo)
puts "I still need to visit the following cities: " + (cities -
visited).join(",")

martin


More information about the vit-discuss mailing list