Stick it in Your ~/.irbrc: MethodFinder #
Ooh, nice ripoff from Smalltalk. A class called MethodFinder for looking up methods based on what they should return.
You use MethodFinder.show( "hello", 5 )
and it’ll suggest the length
and size
methods. Maybe we could hack it to be a bit more terse?
>> "hello".what? 5 "hello".length 5 “hello”.size == 5 => [“length”, “size”] >> “foo”.what?(“bar”) "foobar" "foo".<<("bar") “foobar” “foo”.+(“bar”) "foobar" "foo".concat("bar") “foobar” => [“<<”, ”+”, “concat”]
Just add to Andrew’s code:
class MethodFinder def initialize( obj, *args ) @obj = obj @args = args end def ==( val ) MethodFinder.show( @obj, val, *@args ) end end class Object def what?(*a) MethodFinder.new(self, *a) end end
In any case, use the syntax easiest to remember. Lest we need a whatwhat?
Ezra
Pretty neat rick. This comes in handy in irb.
Andrew Birkett
That’s very cool. Like it a lot!
why
Medallions go to you, Andrew. I would also encourage you to suppress the warnings by temporarily redirecting
$stderr
why
~/.irbrc
Kian
myavuzselim
Very handy. I start liking ruby :)
But what if you don’t know the order of the parameters? I used the permutation class to make it work for each order of the parameters. here
myavuzselim
J`ey
Kian: class Object def methods_like(regexp) methods.grep(regexp) end end
J`ey
Kian:
musteatyemen
ruby, work smarter not harder?
Jim
for anything I try
jakdak
call me a thicky – but what do i do with this code ?
mathie
Hrm.
Looks like it’s an interaction with the
require 'rubygems'
at the top of my ~/.irbrc.llasram
So this is all fine and dandy in your typical irb session, but when I try it in the Rails console I get:
i.e., irb immediately aborts, all error messages going down with the process.
Any ideas before I start debugging?
llasram
So this is all fine and dandy in your typical irb session, but when I try it in the Rails console I get:
i.e., irb immediately aborts, all error messages going down with the process.
Any ideas before I start debugging?
Brian
I am seeing have the same trouble as reported by Jim and by mathie.
I am on Windows though not sure that matters.
why
Jim, mathie, Brian: That’s really odd. Looks like mathie’s on OSX , though, so it’s not platform-specific. I just tested on Windows and it worked fine like double donkeys. I think we need to catch exceptions and put together a whitelist of desctructive methods (ignoring bangs.)
llasram
My problem turned out to be the Rails “daemonize” method—my irb session wasn’t dying, just slipping into the background.
Adding a
@@black_list
and aselect
onnot @@black_list.include? name
inMethodFinder#find
fixed it.Kian
J`ey: Thanks. Can’t believe I haven’t done it that way all this time… I always forget to look at the Enumerable API when dealing with arrays. Really wish they’d just include those methods on the Array API page!
Vrensk
Wow! I found this just after I spent a few hours (or so it felt) searching for the #camelize method (that turns “dragon_controller” to “DragonController”). #what? would have saved those hours—or so I thought.
It turns out that
does not return #camelize since #camelize has an optional first argument (capiltalize_first = true). So it has an arity of -1. So here’s my suggestion:
Note that ‘what?’ must be in the
@@blacklist
or we will get eternal recursion. Oh, and I moved the STDERR stuff to the find method since that is where the errors are printed.I’ll try to work the permutations in there too. But again: wow!
why
That’s a good Vrensk!
Daniel Schierbeck
I streamlined the code, and changed it to use my preferred coding style (change it back if you want to). I hope it’s ok with Andrew that I post it here; I couldn’t comment on his own entry.
Just add the #what? method yourself.
By the way, is there a better name for #mega_clone ? It sounds kind of odd…
Daniel Schierbeck
Hmmm, it seems your preview function ain’t working—trying again:
I streamlined the code, and changed it to use my preferred coding style (change it back if you want to). I hope it’s ok with Andrew that I post it here; I couldn’t comment on his own entry.
Just add the #what? method yourself.
By the way, is there a better name for #mega_clone ? It sounds kind of odd…
llasram
Daniel: Maybe just
#clone
? Given that you can clone frozen objects, being unable to clone numbers seems odd to me. Even if you actually get a new object, you still can’t change it so it might as well be the old object. Maybe “#clone_if_mutable
”? “#clone_or_self
”?OTOH , I just realized that the code as stands could be rather dangerous if the clonee was (a) mutable but (b) raised an exception in
#clone
. Time to start checking for TypeError in particular?Jim
Forgive me for not being precise, am a nuby. On Windows, I had originally tried under script/console, which is where I spend more time, and where I get the fork error. On IRB it works just fine, warning about Object#type & Object#id being deprecated.
Brian, did you make the same mistake?
Non nubies: what could I do to make it work in the script/console mode?
Daniel Schierbeck
llasram: yeah, I added the TypeError restriction to the rescue clause after posting here.
Daniel Schierbeck
Okay, I’ve boiled it down even further. I’ve removed the output method (I don’t really need it) and I’ve used a block to specify the output requirements. Remember to use the #clone version from my earlier post.
which reminds me; why no rescue clauses on do … end blocks?
Nikolas Coukouma
My recursion prevention hack isn’t as elegant as Vrensk’s :(
bobfoc
is there a version of the .irbrc file that includes all these improvements ?
Nikolas Coukouma
I’ve made a combo version and included some credits.
David Tran
FYI : Steven Grady posted MethodFinder on ruby-talk in 2002.
Based on all people’s programs, I come out mine versions like: (1) simple MethodFinder
(2) permutation arguments MethodFinder
slumos
Now to integrate this with rspec and koders.com. I get direct deposit, so all I need now is a hologram of me typing madly. Hmm, maybe as long as CVS commits keep happening, a cardboard version will work just as well.
Comments are closed for this entry.