Anonym variable $_ and foreach similar in Ruby?

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?

(I am not very knowledgeable guy).
Thanks
ngoc

Or:

array.each { |e| puts e }

Or just:

puts array

I like that better than Perl, but you decide... :wink:

James Edward Gray II

···

On Aug 10, 2005, at 2:46 PM, 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?

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

But more logical for this purpose, I think, is

----code---
puts array
----endcode---

ngoc <ngoc@yahoo.com> writes:

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.

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.

···

--
Daniel Brockman <daniel@brockman.se>

ngoc wrote:

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

Actually you can do:

class Proc
  def foreach(ary)
    ary.each(&self)
  end
end

1.9:
->(x){ puts x }.foreach array

<1.9:
lambda { |x| puts x }.foreach array

But "puts array" is perhaps better.

···

--
Florian Frank

James Edward Gray II wrote:

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... :wink:

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.

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 :wink:

....of course it would be much shorter to just say:
  
  puts array

Phil

···

Charles Steinman <acharlieblue@gmail.com> wrote:

Hi --

James Edward Gray II wrote:

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... :wink:

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 :slight_smile: 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
:slight_smile:

David

···

On Thu, 11 Aug 2005, [ISO-8859-1] Florian Groß wrote:

--
David A. Black
dblack@wobblini.net

David A. Black 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 :slight_smile: 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
:slight_smile:

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.