Skip to content

Ruby Blender

  • Blog
  • About
  • Archives
  • Log in
 
Less
More
Trim
Untrim
« Older
Home
Loading
Newer »
Tag Archive for 'utilities'
19Feb09 my_methods revisited
programming ruby
0 Comments

In irbrc goodness, I touched briefly on my_methods, a method of discovering the methods of an object which are distinct from those inherited from Object. The method looks like this:

class Object
def my_methods
(self.methods - Object.methods).sort
end
end

I have since had the idea of extending it — now you can also choose to see only the methods which are implemented by an object’s Class. You can see it in use here (code at the end):
$ irb
>> class Object
>> def my_methods(_super=false)
>> _methods = (_super) ? self.class.superclass.new.methods : Object.methods
>> (self.methods – _methods).sort
>> end
>> end
=> nil
>> class Foo < String >> def fud
>> puts “fud”
>> end
>> end
=> nil
>> f=Foo.new
=> “”
>> f.my_methods
=> [“%”, “*”, “+”, “<<", "[]", "[]=", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "collect", "concat", "count", "crypt", "delete", "delete!", "detect", "downcase", "downcase!", "dump", "each", "each_byte", "each_line", "each_with_index", "empty?", "entries", "find", "find_all", "fud", "grep", "gsub", "gsub!", "hex", "index", "inject", "insert", "intern", "is_binary_data?", "is_complex_yaml?", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "member?", "min", "next", "next!", "oct", "partition", "reject", "replace", "reverse", "reverse!", "rindex", "rjust", "rstrip", "rstrip!", "scan", "select", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "strip", "strip!", "strip_tags", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "to_f", "to_i", "to_str", "to_sym", "tr", "tr!", "tr_s", "tr_s!", "unpack", "upcase", "upcase!", "upto", "zip"] >> f.my_methods(true)
=> [“fud”]
>>

Here’s the promised code:
class Object
def my_methods(_super=false)
_methods = (_super) ? self.class.superclass.new.methods : Object.methods
(self.methods - _methods).sort
end
end

 
Browse Archives »
  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)
 

Recent Posts

  • Default values for Attributes
  • Arguments with Trollop
  • Database Paranoia — ActiveRecord Callbacks
  • Ruby Bindings and Scope
  • What’s #method_missing missing?

Search

Browse by Category

  • administrivia (2)
  • configuration (1)
  • metaprogramming (1)
  • mini-saga (5)
  • programming (10)
  • rails (1)
  • ruby (15)
  • rubygems (2)
  • Uncategorized (1)

Browse by Tag

  • ActiveRecord
  • binding
  • Command-line interface
  • gotcha
  • irb
  • irbc
  • Languages
  • Library
  • Local variable
  • metaprogramming
  • mini-saga
  • paradigm
  • philosophy
  • programming
  • rails
  • Recursion
  • ruby
  • rubygems
  • Ruby on Rails
  • scope
  • utilities

Browse by Month

  • September 2009 (1)
  • August 2009 (4)
  • April 2009 (1)
  • February 2009 (11)
 
 
  • Blog
  • About
  • Archives
  • Log in
 


Theme Design by Jay Kwong | Powered by WordPress and K2

 

Home Top Archives Entries FeedComments Feed