Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

appendix_a

  • Loading branch information...
commit 568fb924d45663abb9a7b679c7ba2fa669f01e45 1 parent 4cd5871
@cocoa authored
View
BIN  .DS_Store
Binary file not shown
View
23 appendix_a/_Object_extend.rb
@@ -0,0 +1,23 @@
+# extend(module, ...) → obj
+#
+# Adds to obj the instance methods from each module given as
+# a parameter.
+#
+
+module Mod
+ def hello
+ "Hello from Mod"
+ end
+end
+
+class Klass
+ def hello
+ "Hello from Klass"
+ end
+end
+
+k = Klass.new
+p k.hello
+
+k.extend(Mod)
+p k.hello
View
24 appendix_a/spell_Class_Extension.rb
@@ -0,0 +1,24 @@
+# Class Extension
+#
+
+class C;end
+
+module M
+ def my_method
+ 'a class method...'
+ end
+end
+
+#class << C
+# include M
+#end
+
+class C
+ class << self
+ include M
+ end
+end
+
+
+p C.singleton_methods
+p C.my_method
View
41 appendix_a/spell_Class_Extension_Mixin.rb
@@ -0,0 +1,41 @@
+# Class Extension Mixin (whitout using Hooks)
+#
+
+module ClassMethods
+ def my_method
+ 'a class method'
+ end
+end
+
+class C;end
+
+C.extend(ClassMethods)
+
+p C.singleton_methods
+p C.my_method
+
+
+
+# The same concept but in this case using Hook ClassMethods
+#
+
+module M
+ def self.included(base)
+ puts "help!, I'm being included by #{base}"
+ base.extend(ClassMethods)
+ end
+ module ClassMethods
+ def my_method
+ 'b class method'
+ end
+ end
+end
+
+class D
+ include M
+end
+
+p D.singleton_methods
+p D.my_method
+
+
View
15 appendix_a/spell_Class_Macro.rb
@@ -0,0 +1,15 @@
+# Class Macro
+#
+
+class C; end
+
+class << C
+ def my_macro(*args)
+ "my_macro(#{args.inspect})"
+ end
+end
+
+p class C
+ my_macro :a, :b, :c
+end
+
View
10 appendix_a/spell_Clean_Room.rb
@@ -0,0 +1,10 @@
+# Clean Room
+#
+
+class CleanRoom
+ def a_useful_method(x)
+ x*2
+ end
+end
+
+p CleanRoom.new.instance_eval { a_useful_method(10) }
View
30 appendix_a/spell_Defered_evaluation.rb
@@ -0,0 +1,30 @@
+# Defered evaluation
+#
+
+class C
+ attr_accessor :plus
+
+ def initialize(plus)
+ @plus = plus
+ end
+
+ def store(&block)
+ @my_code_capsule = block
+ end
+
+ def execute
+ @my_code_capsule.call(@plus)
+ end
+end
+
+@plus = 1
+obj = C.new(10)
+
+obj.store { |var| $X = var }
+# obj.store { |var| $X = @plus }
+
+obj.plus= 20
+
+obj.execute
+p obj
+p $X
View
7 appendix_a/spell_Dynamic_Dispatch.rb
@@ -0,0 +1,7 @@
+# Dinamic Dispatch
+#
+
+method_to_call = :reverse
+obj = "abc"
+
+p obj.send(method_to_call)
View
22 appendix_a/spell_Dynamic_Method.rb
@@ -0,0 +1,22 @@
+# Dinamic Method
+#
+
+class C;end
+
+C.class_eval do
+ define_method :my_method do
+ "this is a dynamic method..."
+ end
+end
+
+def define_another_method_in(klass)
+ klass.class_eval do
+ define_method :another_method do
+ "this is another dynamic method..."
+ end
+ end
+end
+
+define_another_method_in(C)
+
+p C.instance_methods(false)
View
16 appendix_a/spell_Dynamic_Proxy.rb
@@ -0,0 +1,16 @@
+# Dynamic Proxy
+#
+
+class DynamicProxy
+ def initialize(target)
+ @proxy_object = target
+ end
+
+ def method_missing(name, *args, &block)
+ "result: #{@proxy_object.send(name,*args,&block)}"
+ end
+end
+
+obj = DynamicProxy.new("buenos dias")
+p obj
+p obj.reverse
Please sign in to comment.
Something went wrong with that request. Please try again.