Hi
In perl
print $_, "\n" foreach (@array);
In Ruby
array.each { |e| print e, "\n" }
or
array.each do |e| print e, "\n" end
So |e| is similar to $_. But I can not
say
{ |e| print e, "\n"} array.each
because of block as argument and OO issue. In addition, it looks a little bit complicated in Ruby. I think in THIS CASE perl is more elegant than Ruby?
Is it a right thinking (IN THIS CASE)?
Are there other ways than those two array.each?
Hi
In perl
print $_, "\n" foreach (@array);
In Ruby
array.each { |e| print e, "\n" }
or
array.each do |e| print e, "\n" end
So |e| is similar to $_. But I can not
say
{ |e| print e, "\n"} array.each
because of block as argument and OO issue. In addition, it looks a little bit complicated in Ruby. I think in THIS CASE perl is more elegant than Ruby?
Is it a right thinking (IN THIS CASE)?
Are there other ways than those two array.each?
Hi
In perl
print $_, "\n" foreach (@array);
In Ruby
array.each { |e| print e, "\n" }
or
array.each do |e| print e, "\n" end
So |e| is similar to $_. But I can not
say
{ |e| print e, "\n"} array.each
because of block as argument and OO issue. In addition, it looks a
little bit complicated in Ruby. I think in THIS CASE perl is more
elegant than Ruby?
Is it a right thinking (IN THIS CASE)?
Are there other ways than those two array.each?
There is also
----code---
for e in array; puts e end
----endcode---
The following syntax comes out naturally for Ruby,
print e, "\n" for e in array
but Matz does not want to allow it because he believes that
referencing a variable before binding it is too confusing.
In addition, it looks a little bit complicated in Ruby.
I think in THIS CASE perl is more elegant than Ruby?
Although I do loathe the forced dummy variable `e', I think
you could come up with lots of better examples of where Perl
code is more elegant than the equivalent Ruby.
In article <1123703873.374539.125830@g47g2000cwa.googlegroups.com>,
ngoc wrote:
Hi
In perl
print $_, "\n" foreach (@array);
In Ruby
array.each { |e| print e, "\n" }
or
array.each do |e| print e, "\n" end
So |e| is similar to $_. But I can not
say
{ |e| print e, "\n"} array.each
because of block as argument and OO issue. In addition, it looks a
little bit complicated in Ruby. I think in THIS CASE perl is more
elegant than Ruby?
Is it a right thinking (IN THIS CASE)?
Are there other ways than those two array.each?
There is also
----code---
for e in array; puts e end
----endcode---
and for the original poster who seemed to prefer:
print $_, "\n" foreach (@array);
(for whatever reason)
He could even say:
puts for item in array; end
If he really wanted to
....of course it would be much shorter to just say:
In perl
print $_, "\n" foreach (@array);
Are there other ways than those two array.each?
Or:
array.each { |e| puts e }
Or just:
puts array
I like that better than Perl, but you decide...
And the general form for guys who are too lazy to pass along arguments:
[1, 2, 3].each(&method(:puts))
But I think ary.each { |e| puts e } is the Ruby way.
I don't; I advocate doing
puts ary
and let Ruby do the work I know, I know... it's not as explicit
in its handling of each element. But once someone tells you how it
works, you know; and I think learning that is one of the Ruby rites of
passage (like learning that #{...} interpolates more than variables
David
···
On Thu, 11 Aug 2005, [ISO-8859-1] Florian Groß wrote:
And the general form for guys who are too lazy to pass along arguments:
[1, 2, 3].each(&method(:puts))
But I think ary.each { |e| puts e } is the Ruby way.
I don't; I advocate doing
puts ary
and let Ruby do the work I know, I know... it's not as explicit
in its handling of each element. But once someone tells you how it
works, you know; and I think learning that is one of the Ruby rites of
passage (like learning that #{...} interpolates more than variables
In this case I would also use puts ary, of course, but there is cases like
%w(foo bar qux).each { |file| require file }
where you would also be able to do
%w(foo bar qux).each(&method(:require))
In that case I would still prefer the more explicit block form.
Sorry, if that didn't come out too clear in my previous post.