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


Groups > comp.lang.ruby > #2548

Re: Calling to_enum on a MatchData object

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!de-l.enfer-du-nord.net!feeder1.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 Fri, 8 Apr 2011 14:21:41 -0500
Organization Service de news de lacave.net
Lines 42
Message-ID <c1dd7962cd9c06e4664e0fe89e6dab6f@ruby-forum.com> (permalink)
References <840b7dcf34f7c1ae6b2b9d80a6b55b69@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 1302292609 42635 65.111.164.187 (8 Apr 2011 19:56:49 GMT)
X-Complaints-To abuse@lacave.net
NNTP-Posting-Date Fri, 8 Apr 2011 19:56:49 +0000 (UTC)
In-Reply-To <840b7dcf34f7c1ae6b2b9d80a6b55b69@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 381215
X-Ml-Name ruby-talk
X-Rubymirror Yes
X-Ruby-Talk <c1dd7962cd9c06e4664e0fe89e6dab6f@ruby-forum.com>
Xref x330-a1.tempe.blueboxinc.net comp.lang.ruby:2548

Show key headers only | View raw


Vahagn Hayrapetyan wrote in post #991813:
> Hi, I have the following snippet of code:
>
> def x
>   File.open('screen.css') do |f|
>     while line = f.gets
>       file = line.match(/"(\w*.css)"/)
>       puts file.class #MatchData
>       puts file.methods #to_enum defined
>       e = file.to_enum
>       puts e.class #Enumerator
>       puts e.methods #each method defined
>       e.each do |entry|
>          puts entry
>       end
>     end
>   end
> end
>
> The result of the regex operation gets stored in "file" as a MatchData
> object. Then I convert it to an Enumerator object, which by all accounts
> has an each method defined. Yet when I try to "e.each" here's what I
> get:
>
> undefined method `each' for #<MatchData "\"reset.css\"" 1:"reset.css">
> (NoMethodError).
>
> Then I check the docs for the MatchData class:
> http://www.ruby-doc.org/core/classes/MatchData.html
>
> and find that no, "to_enum" isn't defined.

to_enum() is defined in Object, and all classes inherit from Object. 
You need to hook up an enumerator's each() method to *an iteration 
method of another object*.  You specify the iteration method as an 
argument to to_enum().  If you don't specify an iteration method, the 
default is each()--and the MatchData class does not define an each() 
method.  Hence your error.

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