From: Ilias Lazaridis Date: 2011-06-05T23:00:31+09:00 Subject: Re: CORE - Altering Behaviour of "each do" (default param "item") On 4 Ιούν, 17:50, James Gray wrote: > [Note:  parts of this message were removed to make it a legal post.] > > On Sat, Jun 4, 2011 at 6:50 AM, Ilias Lazaridis 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 [...] - (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) > http://www.nationmaster.com/encyclopedia/Ilias-Lazaridis 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. . -- http://lazaridis.com