Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2581

Re: Calling to_enum on a MatchData object

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!talisker.lacave.net!lacave.net!not-for-mail
From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: Calling to_enum on a MatchData object
Date Sat, 9 Apr 2011 13:18:03 -0500
Organization Service de news de lacave.net
Lines 84
Message-ID <b5660160be5954fca0117643f7dcb9f1@ruby-forum.com> (permalink)
References <840b7dcf34f7c1ae6b2b9d80a6b55b69@ruby-forum.com> <e6e43a5fecbaa1960938d453c77314ee@ruby-forum.com>
NNTP-Posting-Host bristol.highgroove.com
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-Trace talisker.lacave.net 1302373112 85660 65.111.164.187 (9 Apr 2011 18:18:32 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Sat, 9 Apr 2011 18:18:32 +0000 (UTC)
In-Reply-To <e6e43a5fecbaa1960938d453c77314ee@ruby-forum.com>
X-Received-From This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway
X-Mail-Count 381247
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <b5660160be5954fca0117643f7dcb9f1@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:2581

Show key headers only | View raw


Vahagn Hayrapetyan wrote in post #991914:
> Thank you for your explanations, I can see now that :each is an
> inherited method and it had to be ferreted out of the MatchData set of
> methods.
>

I'm not sure what you are referring to because each() is neither defined 
nor inherited in the MatchData class.  Check the output of:

md = /h/.match('hello')
puts md.methods.sort

And Enumerator defines its own each() method.


> So in order to use an enumerator, an object itself has to define an
> (:each) method and then an enumerator just augments that.

No.  An enumerator can be hooked up to any "iteration method" for an 
object.  An "iteration method" is any method that calls yield().  Here 
is an example:

class Dog
  def bark
    yield 'woof'
    yield 'whooooo'
    yield 'ruuuff'
  end
end

dog = Dog.new
e = dog.enum_for(:bark)

results = e.select{|sound| sound[0] == 'w'}
p results

--output:---
["woof", "whooooo"]


results = e.map{|sound| "#{sound.capitalize}!}" }
p results

--output:--
["Woof!}", "Whooooo!}", "Ruuuff!}"]


if e.include?('woof')
  puts 'yes'
end

--output:--
yes


You may be confusing Enumer-ator with something you read about 
Enumer-able.  If you want to make the Enumer-able methods available to 
your class, e.g. group_by(), each_slice(), select(), map(), include?(), 
etc., then your class has to:

1) Define an each() method, and
2) include Enumerable

Now the confusing part: the Enumer-ator class defines a few of its own 
methods, but it also includes Enumer-able, so an enumerator object has 
both the Enumerator and Enumerable methods available to it.

An enumerator can also be hooked up to a block that you define:

e = Enumerator.new do |y|
  10.upto(14) do |num|
    y << num
  end
end

results = e.map{|x| x + 5}
p results

--output:--
[15, 16, 17, 18, 19]

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-08 13:19 -0500
  Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-08 14:41 -0500
  Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-08 14:21 -0500
  Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-08 20:09 -0500
  Re: Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-09 07:03 -0500
    Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-09 13:18 -0500
      Re: Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-13 06:14 -0500
    Re: Calling to_enum on a MatchData object Gary Wright <gwtmp01@mac.com> - 2011-04-09 14:25 -0500
      Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-09 17:17 -0500
        Re: Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-13 06:28 -0500
          Re: Calling to_enum on a MatchData object Gary Wright <gwtmp01@mac.com> - 2011-04-13 22:44 -0500
  Re: Calling to_enum on a MatchData object 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-09 14:02 -0500
    Re: Calling to_enum on a MatchData object Robert Klemme <shortcutter@googlemail.com> - 2011-04-13 06:45 -0500
      Re: Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-14 11:13 -0500
    Re: Calling to_enum on a MatchData object Vahagn Hayrapetyan <vahagnh@gmail.com> - 2011-04-13 06:25 -0500

csiph-web