# The beauty of Ruby through examples

**URL:** <https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768>\
**Category:** Ruby\
**Created:** [September 29, 2010, 8:41pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768 "2010-09-29T20:41:21Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 2, 2010, 4:11pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/21 "2010-10-02T16:11:01Z")

</div>

On Oct 1, 12:14 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How about some more simple ones like
> 
> 'Do Re Mi Fa So La Ti ’ \* 3
> 
> [125, 450, 250, 1000, 675].sort.reverse.collect{|e| “$#{e}”}.take(3)
> 
> ## ’ hello there '.strip.center(30, ‘-’)
> 
> Posted viahttp://www.ruby-forum.com/.

[125, 450, 250, 1000, 675].sort.reverse.map{|e| “$#{e}”}[0,3]  
==\>[“$1000”, “$675”, “$450”]

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 2, 2010, 5:44pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/22 "2010-10-02T17:44:05Z")

</div>

On Oct 2, 2010, at 10:25 AM, w\_a\_x\_man wrote:

> > but i’d
> > 
> > # Dynamism
> > 
> > p cons
> > 
> > # @name
> > 
> > # def age=(other)
> > 
> > puts %w(1 2 3 4 5).pick  
> > end
> 
> Ruby:
> 
> (9…20).sort\_by{ rand }  
> ==\>[15, 14, 12, 18, 16, 10, 9, 13, 11, 20, 17, 19]

And in Ruby 1.9 it’s even easier to shuffle and array:

irb\> (9…20).to\_a.shuffle  
=\> [15, 11, 20, 10, 9, 18, 13, 19, 16, 17, 14, 12]

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 3, 2010, 1:38am UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/23 "2010-10-03T01:38:08Z")

</div>

I would go with #first, since it’s somewhat more readable.

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 3, 2010, 11:22am UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/24 "2010-10-03T11:22:41Z")

</div>

On Sep 29, 8:40 pm, Adriano F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Note: parts of this message were removed to make it a legal post.]

I’ve been collecting some from around the internet, but i’d

> appreciate your suggestions as well.

I started some samples to easily reply to the (programmers) guys  
who ask me “Why Ruby?”.

```
http://www.ensta.fr/~diam/ruby/index.php?id=survie

```

I would be a good idea to build one or several page (wiki?)  
in the style of:  
[http://pleac.sourceforge.net/pleac\_ruby](http://pleac.sourceforge.net/pleac_ruby)  
but with your need in mind!

– Maurice

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 3, 2010, 4:55pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/25 "2010-10-03T16:55:43Z")

</div>

> I would be a good idea to build one or several page (wiki?)  
> in the style of:  
> Â [http://pleac.sourceforge.net/pleac\_ruby](http://pleac.sourceforge.net/pleac_ruby)  
> but with your need in mind!

This site seems to have a few stylistic problems, as far as common  
idiomatic Ruby goes. For instance there’s a lot of ignoring #each in  
[http://pleac.sourceforge.net/pleac\_ruby/arrays.html](http://pleac.sourceforge.net/pleac_ruby/arrays.html)

Might not be the best introduction to Ruby for someone.

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 4, 2010, 7:39pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/26 "2010-10-04T19:39:52Z")

</div>

> I agree!
> 
> That’s why I said “in the style of”: not the programming style, but  
> the web diffusion style (like a wiki).

Yes, I agree. Sorry, I wasn’t objecting to what you said directly, as  
such, but just generally flagging up some of the website’s contents so  
that other readers (hopefully) don’t start writing camelCase.

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 4, 2010, 10:25am UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/27 "2010-10-04T10:25:33Z")

</div>

On Oct 3, 4:54 pm, Adam P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I would be a good idea to build one or several page (wiki?)  
> > in the style of:  
> > [http://pleac.sourceforge.net/pleac\_ruby](http://pleac.sourceforge.net/pleac_ruby)  
> > but with your need in mind!
> 
> This site seems to have a few stylistic problems, as far as common  
> idiomatic Ruby goes. For instance there’s a lot of ignoring #each
> 
> Might not be the best introduction to Ruby for someone.

I agree!

That’s why I said “in the style of”: not the programming style, but  
the web diffusion style (like a wiki).  
A web page which has a stable and simple URL,  
A web page which one can link to for several years.  
– Maurice

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 6, 2010, 4:01pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/28 "2010-10-06T16:01:57Z")

</div>

find the first gap in array of fixnum values

p [1,2,3,5,6,8,9,10].inject {|a, e| e == a.next ? e : (break a.next)}

will produce 4

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 19, 2010, 9:21pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/29 "2010-10-19T21:21:22Z")

</div>

On 29 September 2010 20:40, Adriano F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey all,
> 
> I’m about to introduce Ruby to a group of people that is not familiar to the  
> language, and the approach i’d like to take is simply by distilling out a few  
> sample codes. I’ve been collecting some from around the internet, but i’d  
> appreciate your suggestions as well.

So am I, I will give a lightning talk about Ruby in a week.

Do you have any objection if I use some of the examples of this thread ?

It is not easy to find awesome examples when you are used to them, I  
got a hard time finding some.

Best Regards,  
B.D.

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 20, 2010, 9:22am UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/30 "2010-10-20T09:22:44Z")

</div>

On Tue, Oct 19, 2010 at 2:11 PM, Benoit D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

If you’re on OSX, I think this makes for a great example, though I  
usually  
take out the command line args and set the username and password in  
another  
file that I require.

> <https://github.com/tweetstream/tweetstream/blob/master/examples/growl_daemon.rb>

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 25, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/31 "2010-10-25T23:38:22Z")

</div>

On 20 October 2010 09:22, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If you’re on OSX, I think this makes for a great example, though I usually  
> take out the command line args and set the username and password in another  
> file that I require.  
> [tweetstream/examples/growl\_daemon.rb at master · tweetstream/tweetstream · GitHub](http://github.com/intridea/tweetstream/blob/master/examples/growl_daemon.rb)

Seems cool, I tried a little, and found my screen full of Tweets !

However, I will not give the talk on my laptop, and it will be Linux.

But, I’ll mention it as a cool use case.  
Thanks for the link

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 31, 2010, 2:48am UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/32 "2010-10-31T02:48:11Z")

</div>

On Wed, Sep 29, 2010 at 1:40 PM, Adriano F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> actually, such as:
> 
> # Simplicity
> 
> # Expressiveness
> 
> # Productivity
> 
> # Flexibility
> 
> # Dynamism
> 
> # Freedom
> 
> # Happiness

I presented about Ruby last night to my school for the ACM at my school,  
had  
a few examples to show OO and functional paradigms, and an example to  
show  
that it is dynamic. We also did a rails example, we were giving away a  
4gb  
thumb drive, and so made a Rails app to select a winner from a list of  
contestants, then put it on Heroku and let them go sign up for it  
themselves.

Here were my code examples

> **[PresentationFiles/NightOfTheLivingRuby! at master · JoshCheek/PresentationFiles](https://github.com/JoshCheek/PresentationFiles/tree/master/NightOfTheLivingRuby!/)**
>
> master/NightOfTheLivingRuby!/

And video of the presentation

> **[Night of the Living Ruby](http://www.vimeo.com/channels/nightofthelivingruby)**
>
> Halloween 2010, a presentation about the Ruby programming language I gave at Wichita State University for our chapter of the ACM. Slides: http://www.scribd.com/doc/40473667/Slides-for-Night-Of-The-Living-Ruby Code:…

One thing I’ll suggest is to have the code already printed out and  
sitting  
next to you. Then you can have that organic feel of writing it right  
there,  
which I think makes it easier to follow. But you can also save yourself  
a  
lot of headache of forgetting something when coding live. I’ll also  
suggest  
against a Rails example, because there is a lot of stuff going on in  
Rails,  
you have to keep lots of different things in your brain all at once,  
which  
is difficult to do while you are presenting, and I think the Rails  
example  
was the slowest part of the presentation.

---

<div class="post-metadata">

**Author:** ![Adriano\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@Adriano\_F](https://www.ruby-forum.com/u/Adriano_F)\
**Post date:** [October 6, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768/33 "2010-10-06T19:47:49Z")

</div>

I don’t see this as much of a good use of inject, to be honest.

I’d rather see this:

numbers = [1, 2, 3, 4, 6, 7, 8, 9]  
numbers.each\_cons(2) { |x, y| break x if x.succ != y }

or to get rid of the need to explain how break works and what succ is,  
while allowing more readability, perhaps

numbers = [1, 2, 3, 4, 6, 7, 9, 10] # has a gap after 4 and 7!  
gaps = []  
numbers.each\_cons(2) do |x, y|  
gaps \<\< x unless y == x + 1  
end

gaps #=\> [4, 7]

You’ll possibly have to explain each\_cons, but I think it’s easier  
than explaining inject to someone who doesn’t know Ruby or  
programming. You also get to show off post-statement “unless”!

[Previous page](https://www.ruby-forum.com/t/the-beauty-of-ruby-through-examples/195768.md?page=1)
