=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