Created
March 26, 2011 22:29
-
-
Save ashmoran/888700 to your computer and use it in GitHub Desktop.
Example of using method_missing in a Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rspec' | |
| class Cow | |
| class << self | |
| def method_missing(message) | |
| new(message.to_s) | |
| end | |
| end | |
| attr_reader :name | |
| def initialize(name) | |
| @name = name | |
| end | |
| end | |
| describe Cow do | |
| it "has name" do | |
| Cow.new("daisy").name.should eq "daisy" | |
| end | |
| it "lets you create Cows from the class" do | |
| Cow.daisy.name.should eq "daisy" | |
| end | |
| end |
Author
Haha, I didn't realise fashion had changed!
…Sent from my iPhone
On 26 Mar 2011, at 11:13 PM, caius ***@***.*** wrote:
Dude. `class << self` is so 1.8.6.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dude.
class << selfis so 1.8.6.