Skip to content

Instantly share code, notes, and snippets.

@ashmoran
Created March 26, 2011 22:29
Show Gist options
  • Select an option

  • Save ashmoran/888700 to your computer and use it in GitHub Desktop.

Select an option

Save ashmoran/888700 to your computer and use it in GitHub Desktop.
Example of using method_missing in a Class
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
@caius
Copy link

caius commented Mar 26, 2011

Dude. class << self is so 1.8.6.

class Cow
  def self.method_missing(message)
    new(message.to_s)
  end
end

@ashmoran
Copy link
Author

ashmoran commented Mar 27, 2011 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment