Module: Sourcify::Method::Stubs
- Defined in:
- lib/sourcify/lib/sourcify/method.rb
Instance Method Summary collapse
-
#to_raw_source(opts = {}, &body_matcher) ⇒ Object
Returns the raw code enclosed within this method.
-
#to_sexp(opts = {}, &body_matcher) ⇒ Object
Returns the S-expression representation of this method.
-
#to_source(opts = {}, &body_matcher) ⇒ Object
Returns the code representation of this method.
Instance Method Details
#to_raw_source(opts = {}, &body_matcher) ⇒ Object
Returns the raw code enclosed within this method.
class MyMath
def self.sum(x, y)
x + y # (blah)
end
end
MyMath.method(:sum).to_raw_source
>> "def sum(x, y)
>> x + y # (blah)
>> end"
This works for methods defined via Module#define_method as well. Pls see #to_source for options/arguments supported.
123 124 125 |
# File 'lib/sourcify/lib/sourcify/method.rb', line 123 def to_raw_source(opts={}, &body_matcher) # NOTE: this is a stub for the actual one in Methods::ToRawSource end |
#to_sexp(opts = {}, &body_matcher) ⇒ Object
Returns the S-expression representation of this method.
class MyMath
def self.sum(x, y)
x + y # (blah)
end
end
MyMath.method(:sum).to_sexp
>> s(:defn,
>> :sum,
>> s(:args, :x, :y),
>> s(:scope, s(:block, s(:call, s(:lvar, :x), :+, s(:arglist, s(:lvar, :y))))))
This works for methods defined via Module#define_method as well. Pls see #to_source for options/arguments supported.
102 103 104 |
# File 'lib/sourcify/lib/sourcify/method.rb', line 102 def to_sexp(opts={}, &body_matcher) # NOTE: this is a stub for the actual one in Methods::ToSexp end |
#to_source(opts = {}, &body_matcher) ⇒ Object
Returns the code representation of this method. Unlike Method#to_raw_source, the returned code retains only the functional aspects, fluff like comments are stripped off.
class MyMath
def self.sum(x, y)
x + y # (blah)
end
end
MyMath.method(:sum).to_source
# >> "def sum(x, y)
# >> (x + y)
# >> end"
This works for method defined via Module#define_method as well. The following approach of defining method yields exactly the same result as above:
class MyMath
class << self
define_method(:sum) do |x,y|
x + y # (blah)
end
end
end
MyMath.method(:sum).to_source
# >> "def sum(x, y)
# >> (x + y)
# >> end"
The following options are supported:
-
:strip_enclosure
when set to true, strips the method enclosure to get just the meat within.MyMath.method(:sum).to_source(:strip_enclosure => true) # >> "(x + y)"
-
:attached_to
is useful ONLY when a method is defined via Module#define_method, pls see Sourcify::Proc::Stubs#to_source for more info. -
:ignore_nested
is useful ONLY when a method is defined via Module#define_method, pls see Sourcify::Proc::Stubs#to_source for more info. -
an optional
body_matcher
block is supported as well, again this is ONLY useful for method defined via Module#define_method, pls see Sourcify::Proc::Stubs#to_source for more info.
80 81 82 |
# File 'lib/sourcify/lib/sourcify/method.rb', line 80 def to_source(opts={}, &body_matcher) # NOTE: this is a stub for the actual one in Methods::ToSource end |