[Resolved] respond_to? strange behavior

Hello everyones.
I have a class that scans in a folder to search for ruby files which
contain methods. It add every method founded in itself.
In a method of this class a file is loaded and the method calls
correctly specific code for current file’s extension.
But I want to insert a check, because if a file with uncontemplated
exception were passed program should return an appropriate error.
I tried do this with ‘respond_to?’, before to call specific method with
a ‘send’, I check if exist. The problem is that ‘respond_to?’ always
return ‘false’, also if method can properly be called by send.
Can someone help me understand this strange behavior?

Alessandro Bonfanti wrote in post #1164794:

I have a class that scans in a folder to search for ruby files which
contain methods. It add every method founded in itself.

What does that mean? Where does it add methods to and what is a
“method” in this case?

In a method of this class a file is loaded and the method calls
correctly specific code for current file’s extension.

Since you are only searching for Ruby files what does the reference to
the “current file’s extension” mean? I assume there will always be only
one extension (.rb).

But I want to insert a check, because if a file with uncontemplated
exception were passed program should return an appropriate error.

What does that mean?

I tried do this with ‘respond_to?’, before to call specific method with
a ‘send’, I check if exist. The problem is that ‘respond_to?’ always
return ‘false’, also if method can properly be called by send.

What object do you send #respond_to? and what arguments are you passing?
Can you show your code?

Can someone help me understand this strange behavior?

If you can help us understand this strange posting, then maybe. :wink:

I’m very sorry for my bad explanations and my ugly English.
.rb files are only the ruby files where there is code for parse a
specific filetype.

But I want to insert a check, because if a file with uncontemplated
exception were passed program should return an appropriate error.
I mistyped, I want to say “file with uncontemplated extension”.

What object do you send #respond_to? and what arguments are you passing?
Is a call on the implicit object, because explicit call is forbidden by
private declarations. Argument is a symbol converted from a string.

Code is roughly like this:


class Indexer
private

[…some stuff here…]

Dir.entries(“the_directory_name”).each do |entry|
if entry =~ /^parse_\w+.rb$/
require_relative entry
end
end

public

[…some methods here…]

def parse (filepath, filetype = nil)

if filetype == nil
  mn = "parse_#{File.extname(filepath)[1..-1]}"
else
  mn = "parse_#{filetype}"
end

if respond_to? mn.to_sym
  [...some stuff here...]
  self.send mn, filepath
  [...some stuff here...]
else
  [...code that manage the error...]
end

end

end


If I substitute ‘respond_to? mn.to_sym’ with a fake ‘true’ condition,
program works correctly.

I resolved putting a further argument ‘true’ to ‘respond_to?’. It seems
that from Ruby 2.0 ‘respond_to?’ with only an argument check only public
methods.

``self.respond_to? mn.to_sym, true’’ works fine.

I wrote that with a further argument ‘true’ it works fine.
Strangely I was testing if declaring ‘public’ the methods, ‘respond_to?’
works with only one argument, but anyway it don’t work.
I don’t know why.

Frankly, I find the whole design questionable. I would rather store
parsers for different file extensions in a Hash and check for presence
in the hash. For example:

class TextParser
def self.
new(file).parse
end

def initialize(file)
@file = file
end

def parse
puts “Doing more complicated stuff with #{@file}”
end
end

PARSERS = {
‘.rb’ => lambda {|file| puts “parsing Ruby file #{file}”,
‘.bak’ => lambda {|file| puts “ignoring backup #{file}”,
‘.txt’ => TextParser,
}

entries.each do |file|
parser = PARSERS[File.extname(file)]

if parser
parser[file]
else
$stderr.puts “Don’t know how to parse #{file}”
end
end

That avoids generating method names completely and is a much more robust
design.

Btw. you probably want to look at Pathname - that makes file handling
less awkward.

The problem is that ‘respond_to?’ always return ‘false’

This means that your object does not have the method.

There can be no alternative explanation.

x = ‘abc’; x.respond_to? :size # => true
x = ‘abc’; x.respond_to? :size2 # => false

I don’t entirely understand why you use private or public there though -
do you want this to be queryable or not? I mean you could just change it
to public for now and see if it works then.

I’ll pass this to Robert K. because he wanted to know so many
things, now he has to read and answer it. I’d never ask so many
questions back myself! :wink: