[BioRuby] Use of NameError versus respond_to?

Naohisa GOTO ngoto at gen-info.osaka-u.ac.jp
Wed Jul 6 15:16:10 UTC 2011


Hi Andrew,

On Fri, 1 Jul 2011 17:40:04 +1000
Andrew Grimm <andrew.j.grimm at gmail.com> wrote:

> This isn't a bug report, just a request for enlightenment.
> 
> Why does some of the code use begin ... rescue NameError rather than
> respond_to ?

The "begin ... rescue" can skip errors other than NomethodError,
e.g. mismatch of the number of arguments.

> For example, in Bio::Alignment::OriginalPrivate#extract_seq, there's
> 
> for m in [ :seq, :naseq, :aaseq ]
>   begin
>     seq = s.send(m)
>   rescue NameError, ArgumentError
>     seq = nil
>   end
>   break if seq
> end
> 
> Is this because some objects implement seq, naseq or aaseq without
> indicating it in respond_to?

The code can be extend to skip various errors that may occur even
if the method exists, by adding error classes to the "rescue" line.

> Or is this just how code was written back in older times?
> https://github.com/bioruby/bioruby/commit/7b26a41e0a515e77d4b7470936b7fbf68a7b09a8
> indicates the module was first written back in 2003.

Indeed, the code is very old. I'd like to reconsider the design
of the alignment and the sequence classes.

Thanks,

Naohisa Goto
ngoto at gen-info.osaka-u.ac.jp / ng at bioruby.org




More information about the BioRuby mailing list