anonym variable $_ and foreach similar in Ruby ?

N

ngoc

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
 
J

James Edward Gray II

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?

Or:

array.each { |e| puts e }

Or just:

puts array

I like that better than Perl, but you decide... ;)

James Edward Gray II
 
C

Charles Steinman

ngoc said:
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---
 
F

Florian Groß

James said:
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.
 
D

David A. Black

--8323328-821250688-1123705613=:16164
Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-821250688-1123705613=:16164"

This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.

--8323328-821250688-1123705613=:16164
Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE

Hi --

James said:
=20
Or:
=20
array.each { |e| puts e }
=20
Or just:
=20
puts array
=20
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

--=20
David A. Black
(e-mail address removed)
--8323328-821250688-1123705613=:16164--
--8323328-821250688-1123705613=:16164--
 
F

Florian Groß

David said:
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.
 
P

Phil Tomson

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:

puts array


Phil
 
D

Daniel Brockman

ngoc said:
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.
 
F

Florian Frank

ngoc said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top