glob on a dir object

R

ruby newbie

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
 
H

Harold Hausman

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
 
J

Jesse Merriman

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..)
 
R

ruby Newbie

ahh, That makes sense now. I get :: and # syntax that I saw in
Pragmatic programmer's guide .Thank you Harold & Jesse.

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..)
 
C

come

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top