Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 31st, 2012  |  syntax: None  |  size: 0.63 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ##
  2. ## with raw object
  3. ##
  4.  
  5. # create a new object
  6. o = Object.new
  7.  
  8. # define methods on the object (precisely, the object's singleton)
  9. class << o
  10.   def hello(thing)
  11.     puts "hello #{thing}"
  12.   end
  13. end
  14.  
  15. module Yeller
  16.   def hello(thing)
  17.     super thing.upcase
  18.   end
  19. end
  20.  
  21. module Queryer
  22.   def hello(thing)
  23.     super "#{thing}?"
  24.   end
  25. end
  26.  
  27. # apply the Yeller mixin to the object
  28. o.extend Yeller
  29.  
  30. # apply the Queryer mixin to the object
  31. o.extend Queryer
  32.  
  33. o.hello "world" #=> hello WORLD?
  34.  
  35.  
  36. ##
  37. ## with classes
  38. ##
  39.  
  40. class Person
  41.   include Yeller
  42.   include Queryer
  43.  
  44.   def hello(thing)
  45.     puts "hello #{thing}"
  46.   end
  47. end
  48.  
  49. Person.new.hello "world" #=> hello WORLD?