The beauty of Ruby through examples

Yep, nice addition !

Fred

···

Le 29 septembre à 21:50, Joel VanderWerf a écrit :

F. Senault wrote:

File.open("script.rb") do |fichier|
  fichier.each do |ligne|
    if ligne.downcase.include? "ruby"
      puts ligne
    end
  end
end

File.open("script.rb") do |f|
  f.each { |l| puts l if l=~/ruby/i }
end

It's worth mentioning this, too:

puts `grep -i ruby script.rb`

--
I don't know who you are, But you seem very nice
So will you talk to me?
Shall I tell you a story, Shall I tell you a dream?
They think I'm crazy. (K's Choice, Everything for free)

You see, that depends, it is more readanble to me. Probably totally
depends on the context.
For folks coming from functional languages the simple fact of using
inject's synonym might also add to readability

   (1..n).reduce 1, :*

In conclusion I prefer naming things, but the downside is that one has
to communicate names and agree about them. The explicit code block
does not have that disadvantage. However it does not trigger the
information in the brain as quickly as names do, once they are
understood.
This might indeed be one of the forces and beauties of Ruby and it
might be a good idea for OP to show that to his audience.

Cheers
Robert

···

On Thu, Sep 30, 2010 at 9:35 AM, F. Senault <fred@lacave.net> wrote:

Le 29 septembre à 21:26, F. Senault a écrit :

And I forgot a simple, classic, one :

def factorial(n)
(1..n).inject(1) { |p, f| p * f }
end

def factorial(n)
(1..n).inject(1, :*)
end

(I don't like much the second one, it's a bit less legible.)

--
The best way to predict the future is to invent it.
-- Alan Kay

def factorial(n)
(1..n).inject(1, :*)
end
(I don't like much the second one, it's a bit less legible.)

lose the 1.
on my case, i do like the second very much. more idiomatic. less prone
to typo. less to think (convention ie).

i am referring of course, not to the factorial, but of the idiom,

   enum.inject :operator
   enum.inject factor, :operator

eg,

multiplying

(1..5).inject :*

=> 120

(2..5).inject :*

=> 120

(3..5).inject :*

=> 60

adding

(4..5).inject( :+ )

=> 9

multiply w factor

(3..5).inject 5, :*

=> 300

adding w initial,

(4..5).inject( 100, :+ )

=> 109

powers,

(2..3).inject :**

=> 8

(2..4).inject :**

=> 4096

and more fun using blocks..

best regards -botp

···

On Thu, Sep 30, 2010 at 3:35 PM, F. Senault <fred@lacave.net> 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 via http://www.ruby-forum.com/.

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

···

On Oct 3, 4:54 pm, Adam Prescott <mention...@gmail.com> wrote:

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

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 20 October 2010 09:22, Josh Cheek <josh.cheek@gmail.com> 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.
http://github.com/intridea/tweetstream/blob/master/examples/growl_daemon.rb

Except if you want factorial of 0 to be right.

While showing a cool example, I would go to:

class Integer
  def !
    (1..self).inject 1, :*
  end
end

0.! # => 1

···

On 30 September 2010 10:32, botp <botpena@gmail.com> wrote:

On Thu, Sep 30, 2010 at 3:35 PM, F. Senault <fred@lacave.net> wrote:

def factorial(n)
(1..n).inject(1, :*)
end
(I don't like much the second one, it's a bit less legible.)

lose the 1.

-botp

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

···

On Oct 1, 12:14 am, John Sikora <john.sik...@xtera.com> 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/.

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 Mon, Oct 4, 2010 at 9:25 AM, mdiam <maurice.diamantini@gmail.com> wrote:

On Oct 3, 4:54 pm, Adam Prescott <mention...@gmail.com> wrote:

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

my bad. i must review my math now :slight_smile:
best regards -botp

···

On Thu, Sep 30, 2010 at 4:58 PM, Benoit Daloze <eregontp@gmail.com> wrote:

Except if you want factorial of 0 to be right.

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

···

On Sat, Oct 2, 2010 at 3:10 PM, w_a_x_man <w_a_x_man@yahoo.com> wrote:

On Oct 1, 12:14 am, John Sikora <john.sik...@xtera.com> 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"]

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Wed, Oct 6, 2010 at 3:04 PM, Giampiero Zanchi <cidza@tin.it> wrote:

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
--
Posted via http://www.ruby-forum.com/\.