Let’s say I have a Ruby object called Foo. This code will result in the following:
Foo.bar.baz #=> "bar baz"
How could I achieve this. (I know this seems pointless and probably breaks several conventions, I was just curious to see how this could be achieved.
A horrible hackey way to do this horrible hackey thing would be to use method_missing.
Now you can do:
What this says is if you pass an instance of
Oucha method it doesn’t know (anything but run), take the new of that method and append it to the log string stored as an instance variable (@log) inside your instance. Finally, you need some extractor function like run to let the object know that you are done and would like to return the accumulated log. Hope this helps.[EDIT]
To be entirely clear, method_missing is a “magic” Ruby function that gets called whenever a method is called on an object that the object does not recognize.