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