The beauty of Ruby through examples

On Oct 1, 12:14 am, John S. [email protected] 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”]

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.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

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

On Sep 29, 8:40 pm, Adriano F. [email protected] 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
but with your need in mind!

– Maurice

I would be a good idea to build one or several page (wiki?)
in the style of:
 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

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

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.

On Oct 3, 4:54 pm, Adam P. [email protected] wrote:

I would be a good idea to build one or several page (wiki?)
in the style of:
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

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

On 29 September 2010 20:40, Adriano F. [email protected] 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.

On Tue, Oct 19, 2010 at 2:11 PM, Benoit D. [email protected]
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.

On 20 October 2010 09:22, Josh C. [email protected] 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

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

On Wed, Sep 29, 2010 at 1:40 PM, Adriano F. [email protected]
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

And video of the presentation

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.

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