CORE - Altering Behaviour of "each do" (default param "item")

  • Thread starter Ilias Lazaridis
  • Start date
I

Ilias Lazaridis

1.9

names = ["Jane", "Michele", "Isabella"]

# current behaviour
names.each do |name|
print name
end

#Question: How can I alter the behaviour of "each" in the following
way:
names.each do
print item # "item" is used by default
end

Is this possible, without going to C-level?

..
 
M

Michael Sokol

Actually it would be possible, although very ugly.

You need to evaluate in the context of the current instance (instance_eval) -=
not yield - the block you pass to the each method of your object (an array i=
n this case).

Then you can define a print method that behaves like a coroutine (check out r=
uby fibers) and returns each element of your array.

Sorry I'm not writing code here, I'm using my cellphone.

Micha=C3=ABl Sokol
 
P

Panagiotis Atmatzidis

Hello,

1.9
=20
names =3D ["Jane", "Michele", "Isabella"]
=20
# current behaviour
names.each do |name|
print name
end
=20
#Question: How can I alter the behaviour of "each" in the following
way:
names.each do
print item # "item" is used by default
end
=20
Is this possible, without going to C-level?
=20
.
=20

Can't you just write your own implementation of .each?

(along with every other method you found dislike the way it works)

best regards

--
Panagiotis Atmatzidis

email: (e-mail address removed)
blog: http://www.convalesco.org

The wise man said: "Never argue with an idiot. They bring you down to =
their level and beat you with experience."
 
J

James Gray

[Note: parts of this message were removed to make it a legal post.]

1.9

names = ["Jane", "Michele", "Isabella"]

# current behaviour
names.each do |name|
print name
end

#Question: How can I alter the behaviour of "each" in the following
way:
names.each do
print item # "item" is used by default
end

Is this possible, without going to C-level?

Since everyone is still speaking to Ilias far too often, I'll try to use
this as a teaching experiment. I'll post the code that does exactly what he
asked for. Exactly. If he follows standard form, you should see complaints
about the non-code content of this message and the solution itself.

The solution:

#######################################################
### NEVER USE THIS CODE, unless your name is Ilias! ###
#######################################################

class Item
def initialize(item)
@item = item
end

attr_reader :item
end

class Array
alias_method :sane_each, :each
def each(&iterator)
if iterator.arity.zero?
sane_each do |item|
Item.new(item).instance_eval(&iterator)
end
else
sane_each(&iterator)
end
end
end

names = %w[Jane Michele Isabella]

names.each do |name|
puts name
end

names.each do
puts item
end

__END__

The most important thing to learn about Ilias is that he's not a programmer
and has no desire to become one. Ever. Look at his code from this example.
Ilias never posts project code. He doesn't do anything. He just attacks
esoteric corners of syntax to incite debate.

His requests almost always involve terrible ideas that obviously violate
some good programming practice. If he doesn't know this, he has never
learned it from all the years he has spent in many programming language
groups. If he does know it, he's using his knowledge optimally to push the
most buttons. Either way, you know what you need to know.

Note how he's never learned the tiniest bit of Ruby during any of his stays
with us. He doesn't indent like we do, he wouldn't think to call puts(),
etc. Ilias literally cannot assimilate, beyond minor efforts to gain
sympathizers.

See his other messages for examples of how he usually selects the most
abrasive language. Words like "BARRIER" are used for a desired effect and
he loves it when we tell him not to talk to us that way.

You all know that I try very hard to be polite and helpful. Ilias is the
exception. I totally believe he is diagnosable. Anytime we push "Reply,"
we feed his needs. It's hard, but you really have to ignore him. It's the
only option he leaves to us.

You won't see me respond when he complains about this message. He'll say
stupid things I'm dying to correct for all the watching eyes. That's what
Ilias does. Though it hurts me, I won't push "Reply." Just writing a
message like this about a person kill's me. But these are the only ways to
minimize his reach. We are not playing this game against a rational
opponent.

Choose another soul to save on Ruby Talk and let this one go.

http://www.nationmaster.com/encyclopedia/Ilias-Lazaridis

James Edward Gray II
 
B

Bill Felton

=20
Wow -- his own encyclopedia entry.
=20
It'll be difficult for me to refrain from replying, because I almost
never check who says something before responding. I have cultivated = the
habit of responding based on content rather than name, for the most = part.
=20
It's pretty clear that, in this case, I should make the effort to do
otherwise. Thanks for the informative email, James.
=20

Chad, you might want to see if your mail client has the equivalent of a =
killfile. Amusingly enough, I 'killfiled' Ilias this morning before the =
latest kerfuffle.
James, thanks for a careful and well-thought-out response.

All other issues aside, "Dr. Lizardo" appears to want to use a language =
that is almost but not quite completely unlike the language he has =
insisted he 'must' or 'strongly desires' to use. Certainly he is after =
a 'Ruby' that is nothing at all like the Ruby we know and love.
One must wonder why...

regards,
Bill=
 
P

Panagiotis Atmatzidis

go it...
=20
1.9
=20
names =3D ["Jane", "Michele", "Isabella"]
=20
# current behaviour
names.each do |name|
print name
end
=20
#Question: How can I alter the behaviour of "each" in the following
way:
names.each do
print item # "item" is used by default
end
=20
Is this possible, without going to C-level?
=20
=20
Since everyone is still speaking to Ilias far too often, I'll try to = use
this as a teaching experiment. I'll post the code that does exactly = what he
asked for. Exactly. If he follows standard form, you should see = complaints
about the non-code content of this message and the solution itself.
=20
The solution:
=20
#######################################################
### NEVER USE THIS CODE, unless your name is Ilias! ###
#######################################################
=20
class Item
def initialize(item)
@item =3D item
end
=20
attr_reader :item
end
=20
class Array
alias_method :sane_each, :each
def each(&iterator)
if iterator.arity.zero?
sane_each do |item|
Item.new(item).instance_eval(&iterator)
end
else
sane_each(&iterator)
end
end
end
=20
names =3D %w[Jane Michele Isabella]
=20
names.each do |name|
puts name
end
=20
names.each do
puts item
end
=20
__END__
=20
The most important thing to learn about Ilias is that he's not a = programmer
and has no desire to become one. Ever. Look at his code from this = example.
Ilias never posts project code. He doesn't do anything. He just = attacks
esoteric corners of syntax to incite debate.
=20
His requests almost always involve terrible ideas that obviously = violate
some good programming practice. If he doesn't know this, he has never
learned it from all the years he has spent in many programming = language
groups. If he does know it, he's using his knowledge optimally to = push the
most buttons. Either way, you know what you need to know.
=20
Note how he's never learned the tiniest bit of Ruby during any of his = stays
with us. He doesn't indent like we do, he wouldn't think to call = puts(),
etc. Ilias literally cannot assimilate, beyond minor efforts to gain
sympathizers.
=20
See his other messages for examples of how he usually selects the most
abrasive language. Words like "BARRIER" are used for a desired effect = and
he loves it when we tell him not to talk to us that way.
=20
You all know that I try very hard to be polite and helpful. Ilias is = the
exception. I totally believe he is diagnosable. Anytime we push = "Reply,"
we feed his needs. It's hard, but you really have to ignore him. = It's the
only option he leaves to us.
=20
You won't see me respond when he complains about this message. He'll = say
stupid things I'm dying to correct for all the watching eyes. That's = what
Ilias does. Though it hurts me, I won't push "Reply." Just writing a
message like this about a person kill's me. But these are the only = ways to
minimize his reach. We are not playing this game against a rational
opponent.
=20
Choose another soul to save on Ruby Talk and let this one go.
=20
http://www.nationmaster.com/encyclopedia/Ilias-Lazaridis
=20
James Edward Gray II

--
Panagiotis Atmatzidis

email: (e-mail address removed)
blog: http://www.convalesco.org

The wise man said: "Never argue with an idiot. They bring you down to =
their level and beat you with experience."
 
M

Martin DeMello

All other issues aside, "Dr. Lizardo" appears to want to use a language t=
hat is almost but not quite completely unlike the language he has insisted =
he 'must' or 'strongly desires' to use. =A0Certainly he is after a 'Ruby' t=
hat is nothing at all like the Ruby we know and love.
One =A0must wonder why...

I wonder how well io or scala could be adapted to his needs - they
have somewhat more flexible syntax than ruby in that regard.

martin
 
M

Michael Sokol

[Note: parts of this message were removed to make it a legal post.]

I really wonder what that "Dr. Lizardo" ( ;)) is trying to achieve. Is he legitimately, in his own way, trying to explore the language and grok each and every edge-cases? That's what I thought at first. I didn't approve the way he answered to people helping him (very rude most of the time), nor the way he exposes his questions. But wow, that encyclopedia entry says that the article has been tagged since 2006!

Could it be a real internet troll we have here?
 
M

Matthias Wächter

James,

His requests almost always involve terrible ideas that obviously violate
some good programming practice.

FWIW, his three BARRIER requests all were accepted and solved on ruby-core, and his latest request for "executed?" – which I personally consider a
good idea – looks like to go the same way.

I nevertheless find his style of running personal attacks out of nowhere very counter productive and offending. Someone should tell him that he could
try to change the world with other tools than chainsaws and atomic bombs. Must be some kind of bad childhood.

– Matthias
 
D

David Masover

I wonder how well io or scala could be adapted to his needs - they
have somewhat more flexible syntax than ruby in that regard.

Unlikely. As James pointed out, he doesn't appear to be a programmer, judging
by some of his responses complaining about the actual answer being "too
complicated." His "needs" don't seem to be related to actual programming
problems.

But I'm just guessing, because as much as he refuses to actually learn
anything in a strictly technical context, he hasn't been willing to discuss
_anything_ in _any_ context. There'd be a "BARRIER" within a week in pretty
much any language he didn't write himself.
 
R

Robert Klemme

Actually it would be possible, although very ugly.

You need to evaluate in the context of the current instance
(instance_eval) - not yield - the block you pass to the each method
of your object (an array in this case).

Then you can define a print method that behaves like a coroutine
(check out ruby fibers) and returns each element of your array.

There is another approach which does not suffer from the side effect of
changing self which can be disastrous: create a thread local which holds
a stack of values and make item peek at the top element of it:

class Object
def new_each
stack = (Thread.current[:__item__] ||= [])

each do |x|
stack.push x
begin
yield
ensure
stack.pop
end
end

self
end

private
def item
stack = Thread.current[:__item__] or
raise "Not in new_each"
stack.last
end
end

a = %w{a b c}
b = [1, 2, 3]

a.new_each do
printf "%-20s: %p\n", 'outer 1', item

b.new_each do
printf "%-20s: %p\n", 'inner', item
end

printf "%-20s: %p\n", 'outer 2', item
end

This approach
- does not change 'self'
- is thread safe
- is nesting safe

Kind regards

robert
 
I

Ilias Lazaridis

Actually it would be possible, although very ugly.
You need to evaluate in the context of the current instance
(instance_eval) - not yield - the block you pass to the each method
of your object (an array in this case).
Then you can define a print method that behaves like a coroutine
(check out ruby fibers) and returns each element of your array.

There is another approach which does not suffer from the side effect of
changing self which can be disastrous: create a thread local which holds
a stack of values and make item peek at the top element of it:

class Object
   def new_each
     stack = (Thread.current[:__item__] ||= [])

     each do |x|
       stack.push x
       begin
         yield
       ensure
         stack.pop
       end
     end

     self
   end

private
   def item
     stack = Thread.current[:__item__] or
       raise "Not in new_each"
     stack.last
   end
end

a = %w{a b c}
b = [1, 2, 3]

a.new_each do
   printf "%-20s: %p\n", 'outer 1', item

   b.new_each do
     printf "%-20s: %p\n", 'inner', item
   end

   printf "%-20s: %p\n", 'outer 2', item
end

This approach
- does not change 'self'
- is thread safe
- is nesting safe

Kind regards

        robert
 
I

Ilias Lazaridis

Actually it would be possible, although very ugly.
You need to evaluate in the context of the current instance
(instance_eval) - not yield - the block you pass to the each method
of your object (an array in this case).
Then you can define a print method that behaves like a coroutine
(check out ruby fibers) and returns each element of your array.

There is another approach which does not suffer from the side effect of
changing self which can be disastrous: create a thread local which holds
a stack of values and make item peek at the top element of it:

class Object
   def new_each
     stack = (Thread.current[:__item__] ||= [])

what does "(Thread.current[:__item__] ||= [])" do?
     each do |x|
       stack.push x [...]

This approach
- does not change 'self'
- is thread safe
- is nesting safe

This sounds good.

But you suggestion adds Class#new_each.

The goal was, to alter the existent behaviour of Array#each.

I've tried to modify your code to achieve this, but I failed.

..
 
R

Robert Klemme

Actually it would be possible, although very ugly.
You need to evaluate in the context of the current instance
(instance_eval) - not yield - the block you pass to the each method
of your object (an array in this case).
Then you can define a print method that behaves like a coroutine
(check out ruby fibers) and returns each element of your array.

There is another approach which does not suffer from the side effect of
changing self which can be disastrous: create a thread local which holds
a stack of values and make item peek at the top element of it:

class Object
def new_each
stack = (Thread.current[:__item__] ||= [])

what does "(Thread.current[:__item__] ||= [])" do?

You ask for things that modify the language and need meta programming in
its implementation yet you do not seem to be comfortable with basic
language functionality - or willing to find out. I sense a certain
contradiction here.
each do |x|
stack.push x [...]

This approach
- does not change 'self'
- is thread safe
- is nesting safe

This sounds good.

But you suggestion adds Class#new_each.

It doesn't. It adds Object#new_each.
The goal was, to alter the existent behaviour of Array#each.

Ilias, this is not the goal assignment channel. You have your goals,
other people have different goals.
I've tried to modify your code to achieve this, but I failed.

Why don't you just post your code and let people comment on it? If you
think about this failure you might find out why making Ruby more like
Groovy in this respect might not be such a good idea altogether. If you
read documentation of Enumerable you will see why it is a bad idea to
attempt this with method #each.

Cheers

robert
 
I

Ilias Lazaridis

[...] - (off topic, off context, processing)

Sad, looked like a promising solution.

..
 
M

Matthias Wächter

[...] - (off topic, off context, processing)
Sad, looked like a promising solution.

Yes, Ilias, it looked like Cockaigne but it isn’t, if just for the matter of Robert’s grilled geese don’t fly into your mouth. In fact, they are too
big with their fine, rosted skin so you need to take them apart by hand. If that’s too much work for you, it’s your problem, not Robert’s and
certainly not the Goose’s.
 
I

Ilias Lazaridis

[Note:  parts of this message were removed to make it a legal post.]

names = ["Jane", "Michele", "Isabella"]
# current behaviour
names.each do |name|
 print name
end
#Question: How can I alter the behaviour of "each" in the following
way:
names.each do
 print item         # "item" is used by default
end
Is this possible, without going to C-level?

Since
[...] - (off topic)
class Item
  def initialize(item)
    @item = item
  end

  attr_reader :item
end

class Array
  alias_method :sane_each, :each
  def each(&iterator)
    if iterator.arity.zero?
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_method.html#Method.arity

      sane_each do |item|
        Item.new(item).instance_eval(&iterator)

Creating an object for each iteration.
Can this be avoided somehow?
      end
    else
      sane_each(&iterator)
    end
  end
end

names = %w[Jane Michele Isabella]

names.each do |name|
  puts name
end

names.each do
  puts item
end

__END__

This looks very good, and seems to work as expected.
The most important thing to learn
[...] - (off-topic, crossing the line)

Have you checked the validity of this information? It was copied from
a wikipedia "article" (the original article was deleted, due to
violation of the wikipedia policies).

The most important thing to learn is:

when is it "expressing negative feelings" and when is it "defamation
of character":

http://www.lawinfo.com/fuseaction/Client.lawarea/categoryid/1162

See, I'm looking in parallel for a contract within the ruby domain (as
I've chosen ruby for my own projects and thus I like to avoid to work
in another languages).

If you (people) continue to attack me on a *professional* level, I'll
have to react at some point.

So, please, set a filter, don't read or go get a dog.

Or stay calm and focus on the technical stuff.

..
 
M

Matthias Wächter

Or stay calm and focus on the technical stuff.

Providing help is a personal matter for many people. You won’t get friends here as long as you snub everyone who is questioning your approach or
motivation. This is help as well and demands proper handling on the personal level from your side, too.

Still, this is *ruby-talk* primarily, even if you are using it over a Usenet gateway. You are free to open up mailing lists named ruby-request or
ruby-support. Let’s see how many people want to subscribe those and focus on your topics in the way and with the level of focus you want.
 

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
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top