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
On 4/24/07, ruby newbie <vigneshsriniv@gmail.com> 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?
------------------------------------------------------------------------
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.
...
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..)
···
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?
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..)
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
···
On 24 avr, 05:58, ruby Newbie <vsrin...@gmail.com> wrote:
ahh, That makes sense now. I get :: and # syntax that I saw in
Pragmatic programmer's guide .Thank you Harold & Jesse.