tags:

views:

38

answers:

2

Hi,

I have taken this example exactly from the Ruby Cookbook. Unfortunately for me, like a whole lot of the examples in that book, this one does not work:

my file (Find.rb - saved both locally and to Ruby\bin):

require 'find'

    module Find
     def match(*paths)
      matched=[]
      find(*paths) { |path| matched << path if yield path }
      return matched
     end
     module_function :match
    end

I try to call it this way from IRB, according to the example the book provides:

irb(main):002:0> require 'Find'
=> false

irb(main):003:0> Find.match("./") { |p| ext = p[-4...p.size]; ext && ext.downcase == "mp3" }

It SHOULD return a list of mp3 files in my recursive directory. Instead, it does this:

NoMethodError: undefined method `match' for Find:Module
        from (irb):3
        from C:/Ruby192/bin/irb:12:in `<main>'

What gives? I'm new at this (although I MUST say that I'm farther along with Python, and much better at it!).

How can I get IRB to use my method?

A: 

The only explanation is that the code you posted is not the code you are running, since both carefully reading it and simply cut&paste&running it shows absolutely no problems whatsoever.

Jörg W Mittag
But the code I pasted IS the code I'm running. Please tell me how can I pass my block to my code?
Rijksband
Hell with it. I'll use Python.
Rijksband
@Rijksband: I just realized that you are on Windows. Windows doesn't distinguish between uppercase and lowercase filenames, so there could potentially be a problem. In fact, if you look at your IRb session above, you can see that `require 'Find'` returns `false`. A return value of `false` means that the library didn't need to be loaded because it was already loaded. Except that in this case, the library that was loaded was `find` not `Find`. So, it's correct: the method *is* undefined, because the file that defines it was never loaded in the first place.
Jörg W Mittag
Yes, that has been the problem all along. How do I get around it and get my Block to call my code?
Rijksband
Please note that, as per my earlier comments, I changed both the name of the function and the name of the file (Froop). Still I get this error:
Rijksband
Rijksband
I can add my Froop.match statement to the end of my .rb and just execute it from cmd. Doesn't work either:
Rijksband
E:\Sounds\Digital>ruby Froop.rbFroop.rb:6:in `match': undefined method `find' for Froop:Module (NoMethodError) from Froop.rb:12:in `<main>'E:\Sounds\Digital>ruby Froop.rbFroop.rb:6:in `match': undefined method `find' for Froop:Module (NoMethodError) from Froop.rb:12:in `<main>'
Rijksband
You know guys, it's a been a very long day and I might be missing some things. Usually I use Python and don't get stuck on things like this for as long as I have in this case. I do appreciate your suggestions but this just isn't working I'm afraid.So thank you for your help anyway.
Rijksband
A: 

What directory are you calling IRB from? Try calling it from the directory where your find.rb file is located. Also, I don't know if it makes any difference but convention is to name the file the lowercase version of the module / class. So the module would be Find and the file name would be find.rb. You shouldn't need the require call in the file itself.

So, start your command prompt window, cd into the directory that contains find.rb and run irb. In IRB you should be able to require "find" and it should return true. From there you should be able to call Find.match.

Adam Tanner