Class: Function
- Inherits:
-
Object
- Object
- Function
- Defined in:
- lib/function.rb
Overview
The Functon module provides python-like functions to ruby.
Instance Method Summary collapse
-
#call(*args) ⇒ Object
(also: #[])
Supposed to quack like Proc here.
-
#initialize(&block) ⇒ Function
constructor
Like Proc, you pass a block to new.
-
#method_missing(meth, *args, &block) ⇒ Object
This allows one to set new attributes so long as you do so using ‘=’.
-
#respond_to?(meth, include_private = false) ⇒ Boolean
Modified to agree with method_missing.
Constructor Details
#initialize(&block) ⇒ Function
Like Proc, you pass a block to new.
17 18 19 20 |
# File 'lib/function.rb', line 17 def initialize(&block) @block = block super() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
This allows one to set new attributes so long as you do so using ‘=’.
24 25 26 27 28 29 30 31 |
# File 'lib/function.rb', line 24 def method_missing(meth, *args, &block) if meth.to_s =~ /^.+=$/ self.class.send(:attr_accessor,meth.to_s.gsub(/=/,'').to_sym) return self.send(meth, *args, &block) else super end end |
Instance Method Details
#call(*args) ⇒ Object Also known as: []
Supposed to quack like Proc here. However, note how it uses instance_exec, which sets the scope to the class instance.
12 13 14 |
# File 'lib/function.rb', line 12 def call(*args) self.instance_exec(*args, &@block) end |
#respond_to?(meth, include_private = false) ⇒ Boolean
Modified to agree with method_missing.
34 35 36 |
# File 'lib/function.rb', line 34 def respond_to?(meth, include_private = false) meth.to_s =~ /^.+=$/ || super end |