Guest User

Untitled

a guest
May 5th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. $VERBOSE = nil
  2. require File.expand_path('../rooby', __FILE__)
  3.  
  4.  
  5. Person = Rooby::Class.new 'Person' do
  6. define :initialize do |name|
  7. @name = name
  8. end
  9.  
  10. define :name do
  11. @name
  12. end
  13. end
  14.  
  15. p Person.new('Alice').name # "Alice"
  16. p Person.new('Bob').name # "Bob"
  17.  
  18.  
  19. JSON = Rooby::Module.new 'JSON' do
  20. define :to_json do
  21. '{"hello": "world"}'
  22. end
  23. end
  24.  
  25. Sum = Rooby::Module.new 'Sum' do
  26. define :wat do |a, b|
  27. a + b
  28. end
  29. end
  30.  
  31. Product = Rooby::Module.new 'Product' do
  32. define :wat do |a, b|
  33. a * b
  34. end
  35. end
  36.  
  37. Log = Rooby::Module.new 'Log' do
  38. define :wat do |a, b|
  39. Math.log(a) / Math.log(b)
  40. end
  41. end
  42.  
  43. Foo = Rooby::Class.new 'Foo' do
  44. include JSON
  45. include Sum
  46.  
  47. metaclass.define :a_singleton_method do
  48. [name, :this_is_cool]
  49. end
  50.  
  51. define :hello do
  52. :hello_there
  53. end
  54.  
  55. define :wat do |a, b|
  56. a ** b
  57. end
  58.  
  59. define :method_missing do |sym, *args|
  60. [:missing, sym, args]
  61. end
  62. end
  63.  
  64. Bar = Rooby::Class.new 'Bar', Foo
  65.  
  66.  
  67. p Foo.ancestors # [Foo, Sum, JSON, Kernel]
  68. p Foo.instance_methods.sort # [:extend, :hello, :inspect, :is_a?, :method, :method_missing, :methods,
  69. # :respond_to?, :to_json, :to_s, :wat]
  70.  
  71. p Foo.a_singleton_method # ["Foo", :this_is_cool]
  72. p Bar.a_singleton_method # ["Bar", :this_is_cool]
  73.  
  74. p Foo.new.is_a?(Foo) # true
  75. p Foo.new.is_a?(Bar) # false
  76. p Bar.new.is_a?(Foo) # true
  77. p Bar.new.is_a?(Bar) # true
  78. p Bar.new.is_a?(JSON) # true
  79.  
  80. p Foo.new.method(:hello) # <Method: <Foo:5af2d6dd>#hello>
  81. p Foo.new.no_method(42) # [:missing, :no_method, [42]]
  82.  
  83. p Foo.new.hello # :hello_there
  84. p Foo.new.to_json # "{\"hello\": \"world\"}"
  85.  
  86. p Foo.new.wat(3,4) # 81
  87. Foo.prepend(Product)
  88. p Foo.ancestors # [Product, Foo, Sum, JSON, Kernel]
  89. p Foo.new.wat(3,4) # 12
  90.  
  91. o = Foo.new
  92. p o.wat(3,4) # 12
  93. o.extend(Log)
  94. p o.wat(3,4) # 0.7924812503605781
Add Comment
Please, Sign In to add comment