Created
March 13, 2011 14:12
-
-
Save marshluca/868115 to your computer and use it in GitHub Desktop.
ancestors.rb
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
#!/usr/bin/env ruby | |
class C | |
end | |
p C.ancestors # => [C, Object, Kernel, BasicObject] | |
# => [C, Object, Kernel] in Ruby 1.8 | |
p Class.ancestors # => [Class, Module, Object, Kernel, BasicObject] | |
# => [Class, Module, Object, Kernel] in Ruby 1.8 |
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
class MyClass | |
def walk | |
p 'walk' | |
end | |
end | |
AnotherClass = Class.new do # just as MyClass.new | |
def run | |
p 'run' | |
end | |
define_method :talk do # dynamic method | |
p 'i am a big mouth' | |
end | |
end | |
# MyClass.new.walk | |
# AnotherClass.new.talk | |
Class.methods.grep /new/ # new is aclass method, not a keyword | |
obj = AnotherClass.new | |
p obj.class | |
p AnotherClass.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
module MiddleMan | |
def say_hello | |
p "Hello from module MiddleMan" | |
end | |
end | |
class Base | |
def say_hello | |
p "Hello from class Base" | |
end | |
end | |
class Test1 < Base | |
end | |
class Test2 < Base | |
include MiddleMan | |
end | |
class Test3 < Base | |
def say_hello | |
p "Hello from class Test3" | |
end | |
include MiddleMan | |
end | |
[Test1, Test2, Test3].each do |klass| | |
klass.new.say_hello | |
end | |
singleton = Test3.new | |
def singleton.say_hello | |
p "Hello from singleton of class Test3" | |
end | |
singleton.say_hello | |
def singleton.method_missing(name, *args, &block) | |
if name == :whoami | |
"a rubyist & solist" | |
else | |
super | |
end | |
end | |
p singleton.whoami | |
p singleton.bang! #undefined method `bang!', from Kernel |
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
module Ability | |
def self.included(base) | |
base.extend ClassMethods | |
base.send(:include, InstanceMethods) | |
end | |
module ClassMethods | |
def has_feature(name, opts={}) | |
if_meth = opts.delete(:if) | |
@features ||= {} | |
@features[name] = if_meth || lambda {|k| true} | |
end | |
def features | |
@features | |
end | |
end | |
module InstanceMethods | |
def has_feature?(feature) | |
if if_meth = self.class.features[feature] | |
if_meth.call(self) | |
else | |
false | |
end | |
end | |
end | |
end | |
class Person | |
include Ability | |
alias has? has_feature? | |
attr_accessor :age | |
has_feature :name, :ignore => 'whatever', :so => 'so what' | |
has_feature :sex, :if => lambda {|c| c.age > 16}, :oops => 'ya' | |
end | |
kid = Person.new | |
kid.age = 12 | |
p kid.has_feature?(:name) | |
p kid.has_feature?(:sex) | |
adult = Person.new | |
adult.age = 17 | |
p adult.has?(:name) | |
p adult.has?(:sex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment