[Ironruby-core] IsRubyMethod

Ivan Porto Carrero ivan at flanders.co.nz
Tue Jul 14 13:39:28 EDT 2009


I guess this could be one way to do it

class Class

  def ruby_method?(name)
    begin
      self.class.clr_member name.to_sym
      return false
     rescue
      return true
     end
  end
end

or something along those lines. I have a thing against relying on exceptions
for these kinds of methods but that will work.
Exceptions are meant for exceptional cases and this isn't an exceptional
case :)

You could also check if the type is a clr type and if it is enumerate the
clr methods with reflection something like this

class String

  # converts a camel cased word to an underscored word
  def underscore
    self.gsub(/::/, '/').
        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
        gsub(/([a-z\d])([A-Z])/,'\1_\2').
        tr("-", "_").
        downcase
  end

end

if ctype = MyClass.to_clr_type
   members = ctype.get_methods + ctype.get_properties
   !members.collect { |mem| mem.to_s.underscore }.include? :my_method
else
   return true
end

---
Met vriendelijke groeten - Best regards - Salutations
Ivan Porto Carrero
Blog: http://flanders.co.nz
Twitter: http://twitter.com/casualjim
Author of IronRuby in Action (http://manning.com/carrero)



On Tue, Jul 14, 2009 at 5:46 PM, Kevin Radcliffe <lists at ruby-forum.com>wrote:

> Shay Friedman wrote:
> > I remember Tomas has posted a code review about IsRubyMethod some time
> > ago.
> > I was looking for it in the code but i couldn't find it. Is it still
> > there?
>
> Shay, I couldn't find it in the source either (unless it is called
> something else)
> In the meantime, perhaps we could come up with a method that does
> something similar that will meet your needs..
> What are you trying to accomplish?
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20090714/3833327b/attachment.html>


More information about the Ironruby-core mailing list