Forum: Ruby glob on a dir object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
ruby newbie (Guest)
on 2007-04-24 03:30
(Received via mailing list)
Hi

I am a Ruby newbie and having fun learning it so far...Can anyone help
me as to why this does not work?

Why does not glob method work on a Directory object created or am I
not using the OO constrcuts the right way?

irb(main):001:0> dh = Dir.new("/tmp/zz")
=> #<Dir:0x64018>
irb(main):002:0>  dh.each { |f| puts "Has file #{f}" }
Has file .
Has file ..
Has file a
Has file aa
Has file b
Has file c
Has file d
=> #<Dir:0x64018>

irb(main):003:0> arr = dh.glob("a?")
NoMethodError: undefined method `glob' for #<Dir:0x64018>
        from (irb):3

Thanks!
VS
Harold H. (Guest)
on 2007-04-24 04:46
(Received via mailing list)
On 4/24/07, ruby newbie <removed_email_address@domain.invalid> wrote:
> Hi
>
> I am a Ruby newbie and having fun learning it so far...Can anyone help
> me as to why this does not work?
>
> Why does not glob method work on a Directory object created or am I
> not using the OO constrcuts the right way?
>

Right.

The glob method is only available on the Dir class. Observe:
C:\>ri Dir#glob
Nothing known about Dir#glob

C:\>ri Dir::glob
-------------------------------------------------------------- Dir::glob
     Dir.glob( string, [flags] ) => array
     Dir.glob( string, [flags] ) {| filename | block }  => nil
------------------------------------------------------------------------
     Returns the filenames found by expanding the pattern given in
     _string_, either as an _array_ or as parameters to the block. Note
     that this pattern is not a regexp (it's closer to a shell glob).
     See +File::fnmatch+ for the meaning of the _flags_ parameter.
   ...


hth,
-Harold
Jesse M. (Guest)
on 2007-04-24 05:00
(Received via mailing list)
On Monday 23 April 2007 19:29, ruby newbie wrote:
> Why does not glob method work on a Directory object created or am I
> not using the OO constrcuts the right way?

glob is a class method of Dir, not an instance method.

irb(main):002:0> Dir::glob('/tmp/zz/a?')
=> ["/tmp/zz/aa"]

If you run 'ri Dir' from the command-line, you can see glob appears
under
'Class methods'. Running 'ri Dir::glob' gives example usage. glob
returns
an array of strings, but if you want Files, you can do this:

irb(main):007:0> Dir::glob('/tmp/zz/a*').map { |f| File.new f }
=> [#<File:/tmp/zz/a>, #<File:/tmp/zz/aa>]

(though perhaps there's a better way; I haven't done too much with
Dir/File
myself..)
ruby Newbie (Guest)
on 2007-04-24 07:59
(Received via mailing list)
ahh, That makes sense now. I get :: and # syntax that I saw in
Pragmatic programmer's guide .Thank you Harold & Jesse.
come (Guest)
on 2007-04-24 11:50
(Received via mailing list)
Hi,

Indeed, "glob" is a classe methods of "Dir" but since Dir is a child
of Enumerable module, you have access to all instance methods of
Enumerable module like, for instance : grep.

So :
dh.grep(/^a?/)

...should play the same role as the glob class method
This topic is locked and can not be replied to.