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

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?

.

Probably not without changing Ruby itself.

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.

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

Michaël Sokol

Hello,

On 04 Ιουν 2011, at 2:50 μ.μ., Ilias L. wrote:

way:

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

email: [email protected]
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.”

On Sat, Jun 04, 2011 at 11:50:00PM +0900, James G. wrote:

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

Wow – his own encyclopedia entry.

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.

It’s pretty clear that, in this case, I should make the effort to do
otherwise. Thanks for the informative email, James.

On Sat, Jun 4, 2011 at 6:50 AM, Ilias L. [email protected]
wrote:

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 T. and let this one go.

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

James Edward G. II

On Jun 4, 2011, at 11:10 AM, Chad P. wrote:

It’s pretty clear that, in this case, I should make the effort to do
otherwise. Thanks for the informative email, James.

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

go it…
On 04 Ιουν 2011, at 5:50 μ.μ., James G. wrote:

this as a teaching experiment. I’ll post the code that does exactly what he
def initialize(item)
sane_each do |item|
names.each do |name|
and has no desire to become one. Ever. Look at his code from this example.
with us. He doesn’t indent like we do, he wouldn’t think to call puts(),
only option he leaves to us.
http://www.nationmaster.com/encyclopedia/Ilias-Lazaridis

James Edward G. II


Panagiotis A.

email: [email protected]
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.”

On Sat, Jun 4, 2011 at 8:47 PM, Bill F.
[email protected] wrote:

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…

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

On Sun, Jun 05, 2011 at 12:17:17AM +0900, Bill F. wrote:

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.

I can just killfile using procmail, if need be. I just tend to be very
reticent to use that capability. I may need to make an exception.

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?

James,

On 04.06.2011 16:50, James G. wrote:

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

On Saturday, June 04, 2011 10:53:13 AM Martin DeMello wrote:

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.

why do all these threads start with CORE ?

On 04.06.2011 16:19, Michael S. wrote:

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

On 4 , 21:02, Robert K. [email protected] wrote:

On 4 , 21:02, Robert K. [email protected] wrote:

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.

.

On 5 Ιούν, 15:13, Robert K. [email protected] wrote:
[…] - (off topic, off context, processing)

Sad, looked like a promising solution.

.

On 05.06.2011 07:53, Ilias L. wrote:

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

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

On 05.06.2011 15:00, Ilias L. wrote:

On 5 Ιούν, 15:13, Robert K.[email protected] wrote:
[…] - (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.