Posted By

Shamaoke on 04/06/11


Tagged

instanceeval classeval


Versions (?)

Scope chain


 / Published in: Ruby
 

Execute an instance method of Object, call in its block class methods of another object and call in the blocks of that methods class or instance methods of the object depending on if the block applies to a macros or to a simple class method.

  1. # encoding: utf-8
  2.  
  3. #####################
  4. # The Example Class #
  5. #####################
  6.  
  7. class Example
  8. def self.execute(&block)
  9. class_eval(&block)
  10. end
  11.  
  12. def self.some_code(&block)
  13. new.instance_eval(&block)
  14. end
  15. end
  16.  
  17. #####################
  18. # Object Extensions #
  19. #####################
  20.  
  21. module ObjectExtensions
  22. def execute(&block)
  23. Example.execute(&block)
  24. end
  25. end
  26.  
  27. ###################################
  28. # Instance Extensions for Example #
  29. ###################################
  30.  
  31. module ExampleInstanceExtensions
  32. def one
  33. 'one'
  34. end
  35.  
  36. def two
  37. 'two'
  38. end
  39.  
  40. def three
  41. 'three'
  42. end
  43. end
  44.  
  45. ########################
  46. # Macroses for Example #
  47. ########################
  48.  
  49. module ExampleClassExtensions
  50. def do_one
  51. some_code do
  52. "do #{one}"
  53. end
  54. end
  55.  
  56. def do_two
  57. some_code do
  58. "do #{two}"
  59. end
  60. end
  61.  
  62. def do_three
  63. some_code do
  64. "do #{three}"
  65. end
  66. end
  67.  
  68. def the_macros
  69. yield
  70. end
  71. end
  72.  
  73. Object.__send__(:include, ObjectExtensions)
  74. Example.__send__(:extend, ExampleClassExtensions)
  75. Example.__send__(:include, ExampleInstanceExtensions)
  76.  
  77. #####################
  78. # The Demonstration #
  79. #####################
  80.  
  81. puts "The #{self} scope." #=> main
  82.  
  83. some_code rescue puts 'some_code boom!' #=> some_code boom!
  84. one rescue puts 'one boom!' #=> one boom!
  85. two rescue puts 'two boom!' #=> two boom!
  86. three rescue puts 'three boom!' #=> three boom!
  87.  
  88. execute do
  89. the_macros do
  90. puts "The #{self} scope." #=> the Example class
  91. puts do_one, #=> do one
  92. do_two, #=> do two
  93. do_three #=> do three
  94. end
  95.  
  96. some_code do
  97. puts "The #{self} scope." #=> an Example instance
  98. puts one, #=> one
  99. two, #=> two
  100. three #=> three
  101. end
  102.  
  103. execute do
  104. some_code do
  105. puts "The #{self} scope." #=> an Example instance.
  106. puts one, #=> one
  107. two, #=> two
  108. three #=> three
  109. end
  110. end
  111.  
  112. puts "The #{self} scope." #=> the Example class
  113. one rescue puts 'one again boom!' #=> one again boom!
  114. two rescue puts 'two again boom!' #=> two again boom!
  115. three rescue puts 'three again boom!' #=> three again boom!
  116. end

Report this snippet  

You need to login to post a comment.