Project

General

Profile

Actions

Feature #440

closed

Better introspection for methods (declaring class, arity)

Added by lkoskela (Lasse Koskela) over 15 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
-
[ruby-core:18288]

Description

=begin
Tools relying on introspection would benefit from having access to information such as:

  1. where was a given method declared, and
  2. how many arguments does a method accept.

Currently, for 1) one needs to parse the output of Object.new.method(:name).to_s to resolve where a method was inherited from.
For 2), one can only get the number of required arguments with Object.new.method(:name).arity, which isn't sufficient if you want to use the optional arguments, too. With the current implementation of arity, one needs to just "assume" that one can throw an arbitrary number of arguments at the method and see if it fails.

The use case for these enhancements is coming from the open source Robot Framework (http://www.robotframework.org). It's a testing harness where you plug in implementations of test cases in the form of "keyword libraries" implemented in Python, Java, or (hopefully soon) Ruby. In order to report available "keywords" (actions available for the test author, implemented as public methods) to the user, the framework needs to scan available keyword libraries for suitable methods and determine how many arguments each keyword accepts (ideally, with the names of those arguments). Improving Ruby's introspection capabilities would be a boost for Ruby's viability as a Robot Framework keyword library implementation language.

  1. would seem like a simple thing to do, given that the information is already available for Method#to_s. I'm not proficient with C, but I might be able to implement that myself as a patch if someone could point me to the right direction regarding the location in the source files.
    =end
Actions #1

Updated by ko1 (Koichi Sasada) over 15 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #2

Updated by ko1 (Koichi Sasada) over 15 years ago

=begin
Current trunk seems to support your requirements. Could you confirm about this issue?
=end

Actions #3

Updated by lkoskela (Lasse Koskela) over 15 years ago

=begin
The first aspect of the issue has been improved via the "source_location" and "receiver" methods being available on the Method object.

It seems to me that the arity stuff, however, is the same as with the 1.8.x branch? That is, aspect 2) of this issue is still current.
=end

Actions #4

Updated by rogerdpack (Roger Pack) over 15 years ago

=begin
if you don't mind a hack then
RubyVM::InstructionSequence.disasm
might help.
Looks like it would be nice to add something to do this, though, since it seems difficult to tell from the #arity call to see how many optional and non optional arguments there are.
Cheers.
-=R
=end

Actions #5

Updated by rogerdpack (Roger Pack) over 14 years ago

  • Status changed from Open to Feedback

=begin
Has this been addressed with the methopara gem and/or http://github.com/maca/arguments
?
=end

Actions #6

Updated by mame (Yusuke Endoh) about 14 years ago

  • Status changed from Feedback to Closed

=begin
Hi,

Tools relying on introspection would benefit from having access to information such as:

  1. where was a given method declared, and
  2. how many arguments does a method accept.

I think the requests has already been solved by Method#source_location
and Method#parameters respectively. So I close this ticket.

--
Yusuke Endoh
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0