Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > CORE - Altering Behaviour of "each do" (default param "item")

Reply
Thread Tools

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

 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      06-04-2011
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?

..

--
http://lazaridis.com
 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      06-04-2011
Probably not without changing Ruby itself.

 
Reply With Quote
 
 
 
 
Michael Sokol
Guest
Posts: n/a
 
      06-04-2011
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

On Jun 4, 2011, at 8:23 AM, Peter Hickman <(E-Mail Removed)> w=
rote:

> Probably not without changing Ruby itself.
>=20


 
Reply With Quote
 
Panagiotis Atmatzidis
Guest
Posts: n/a
 
      06-04-2011
Hello,

On 04 =CE=99=CE=BF=CF=85=CE=BD 2011, at 2:50 =CE=BC.=CE=BC., Ilias =
Lazaridis wrote:

> 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
> --
> http://lazaridis.com
>=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: http://www.velocityreviews.com/forums/(E-Mail 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."






 
Reply With Quote
 
James Gray
Guest
Posts: n/a
 
      06-04-2011
[Note: parts of this message were removed to make it a legal post.]

On Sat, Jun 4, 2011 at 6:50 AM, Ilias Lazaridis <(E-Mail Removed)> wrote:

> 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/encyclop...lias-Lazaridis

James Edward Gray II

 
Reply With Quote
 
Bill Felton
Guest
Posts: n/a
 
      06-04-2011

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

> On Sat, Jun 04, 2011 at 11:50:00PM +0900, James Gray wrote:
>>=20
>> http://www.nationmaster.com/encyclop...lias-Lazaridis

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

 
Reply With Quote
 
Panagiotis Atmatzidis
Guest
Posts: n/a
 
      06-04-2011
go it...
On 04 =CE=99=CE=BF=CF=85=CE=BD 2011, at 5:50 =CE=BC.=CE=BC., James Gray =
wrote:

> On Sat, Jun 4, 2011 at 6:50 AM, Ilias Lazaridis <(E-Mail Removed)> =

wrote:
>=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/encyclop...lias-Lazaridis
>=20
> James Edward Gray II


--
Panagiotis Atmatzidis

email: (E-Mail 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."






 
Reply With Quote
 
Martin DeMello
Guest
Posts: n/a
 
      06-04-2011
On Sat, Jun 4, 2011 at 8:47 PM, Bill Felton
<(E-Mail Removed)> wrote:
>
> 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

 
Reply With Quote
 
Michael Sokol
Guest
Posts: n/a
 
      06-04-2011
[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?
On samedi 4 juin 2011 at 11:17, Bill Felton wrote:
>
> On Jun 4, 2011, at 11:10 AM, Chad Perrin wrote:
>
> > On Sat, Jun 04, 2011 at 11:50:00PM +0900, James Gray wrote:
> > >
> > > http://www.nationmaster.com/encyclop...lias-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.

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


 
Reply With Quote
 
Matthias Wächter
Guest
Posts: n/a
 
      06-04-2011
James,

On 04.06.2011 16:50, James Gray 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML::Template->param() : You gave me an odd number of parameters to param()! Dave Perl Misc 5 04-26-2011 02:44 AM
jsf core tag <f:param> doesnt work with <h:commandButton> but work with <h:commandLink> Janaka Perera Java 0 10-29-2007 08:10 PM
Core Solo & Core Duo are not Core microarchitecture; 65nm Pentium M chips bigal Hardware 0 03-22-2006 11:24 AM
Overload by deriv class param; call w base class param ectoplasm C++ 12 07-28-2005 08:20 AM
XSLT: How to replace param name with this param's value ? Geathaa XML 2 07-30-2003 06:48 AM



Advertisments