~/rubytesting$ cat -n closure1.rb
1 def greetings
2 x = "Goodbye"
3 yield
4 end
5
6 x = "Hello"
7 puts greetings { "#{x} world" }
~/rubytesting$ cat -n blocks1_8.rb
1 def chuchu
2 yield 2
3 end
4
5 x = 1
6 chuchu { |x| }
7 puts x
~/rubytesting$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin10.0]
~/rubytesting$ ruby blocks1_8.rb
~/rubytesting$ cat -n blocks1_8.rb
1 def chuchu
2 yield 2
3 end
4
5 x = 1
6 chuchu { |x| }
7 puts x
~/rubytesting$ sudo rvm install 1.9.2
...
Install of ruby-1.9.2-p290 - #complete
~/rubytesting$
~/rubytesting$ rvm list
rvm rubies
ruby-1.9.2-p290 [ x86_64 ]
~/rubytesting$ rvm ruby-1.9.2-p290
~/rubytesting$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
~/rubytesting$ ruby blocks1_8.rb
RVM
is a command-line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems.
~/rubytesting$ cat -n scopes.rb
1 v1 = 1
2
3 class Tutu
4 v2 = 2
5 puts "%2d"%(__LINE__.to_s)+' '+local_variables.inspect
6
7 def my_method
8 v3 = 3
9 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
10 end
11
12 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
13 end
14
15 obj = Tutu.new
16 obj.my_method
17 obj.my_method
18 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
¿Cuál es la salida de este programa?
~/rubytesting$ ruby scopes.rb 5 ..................... 12 ..................... 9 ..................... 9 ..................... 18 .....................
~/rubytesting$ cat -n instance_scope.rb
1 @var = "hello"
2
3 class Tutu
4 puts "Inside Tutu: #{@var}"
5 end
6
7 puts @var
¿Cual es la salida de esta ejecución?
~/rubytesting$ ruby -w instance_scope.rb
~/rubytesting$ cat -n scopes2.rb
1 v1 = 1
2
3 Tutu = Class.new do
4 v2 = 2
5 puts "%2d"%(__LINE__.to_s)+' '+local_variables.inspect
6
7 def my_method
8 v3 = 3
9 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
10 end
11
12 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
13 end
14
15 obj = Tutu.new
16 obj.my_method
17 obj.my_method
18 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
¿Cuál es la salida de este programa?
~/rubytesting$ ruby scopes2.rb 5 ..................... 12 ..................... 9 ..................... 9 ..................... 18 .....................
~/rubytesting$ cat -n scopes3.rb
1 v1 = 1
2
3 Tutu = Class.new do
4 v2 = 2
5 puts "%2d"%(__LINE__.to_s)+' '+local_variables.inspect
6
7 define_method :my_method do
8 v3 = 3
9 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
10 end
11
12 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
13 end
14
15 obj = Tutu.new
16 obj.my_method
17 obj.my_method
18 puts "%2d"%__LINE__.to_s+' '+local_variables.inspect
¿Cuál es la salida de este programa?
~/rubytesting$ ruby scopes3.rb 5 ..................... 12 ..................... 9 ..................... 9 ..................... 18 .....................
define_method?
define_method? (público, privado, protegido)
define_method?
define_method con un receptor explícito?
Tutu.define_method
~/rubytesting$ cat -n shared_scope2.rb
1 def define_methods
2 shared = 5
3
4 Kernel.send :define_method, :counter do
5 shared
6 end
7
8 Kernel.send :define_method, :inc do |x|
9 shared += x
10 end
11 end
12
13 define_methods
14
15 puts counter
16 inc(4)
17 puts counter
define_method en vez de Kernel.send define_method?
counter e inc?
counter en la línea 15?
MacBook-Air-de-casiano:rubytesting casiano$ cat -n define_method.rb
1 class A
2 def fred
3 puts "In Fred"
4 end
5 def create_method(name, &block)
6 self.class.send(:define_method, name, &block)
7 end
8 define_method(:wilma) { puts "Charge it!" }
9 end
10 class B < A
11 define_method(:barney, instance_method(:fred))
12 end
13 a = B.new
14 a.barney
15 a.wilma
16 a.create_method(:betty) { p self }
17 a.betty
instance_method es definido en Module.
Retorna un UnboundMethod.
Casiano Rodriguez León 2015-01-07