How to specify method default argument using define_method? - ruby

define_method could be used to define methods:
define_method(:m) do |a|
end
which is equivalent to the following:
def m(a)
end
However, what is the equivalent form of the following using define_method:
def m(a=false)
end
Note that I'd need to be able to call m() without giving any argument.

This actually just works like you would expect in Ruby 1.9!
define_method :m do |a = false|
end
If you need 1.8 compatibility, but you don't necessarily need a closure to define your method with, consider using class_eval with a string argument and a regular call to def:
class_eval <<-EVAL
def #{"m"}(a = false)
end
EVAL
Otherwise follow the suggestion in the thread that philippe linked to. Example:
define_method :m do |*args|
a = args.first
end

This is currently not possible due to the yacc parser.
This thread on Ruby-forum proposes several solutions.
class A
define_method(:hello) do | name, *opt_greeting|
option = opt_greeting.first || Hash.new
greeting = option[:greeting] || "hello"
puts greeting+" "+name
end
end
a = A.new
a.hello "barbara"
a.hello "Mrs Jones", :greeting => "Good Morning"

Related

Ruby: Performing additional commands on delegated method

I'd like to use delegate to pass map from a string on to chars, then join back to a string result.
require 'forwardable'
module Map
module String
extend Forwardable
def self.included(base)
base.send :extend, Forwardable
end
# Map for String
delegate map: :chars
end
end
class String
include Map::String
end
As it's originally a string I'd like to perform join after the delegated method has performed its duties. How do I modify the delegate line to include the additional change? The closest thing I've seen online is SimpleDelegator with __setobj__. But that's not well documented and I'm not able to ascertain how to use it for this.
I'm strictly looking for an answer in regards to delegate or SimpleDelegate
The equivalent behavior I'm looking for is this:
module Map
module String
def map(*args, &block)
if (!args.compact.empty? || !block.nil?)
self.chars.map(*args,&block).join
else
self.chars.map(*args,&block)
end
end
end
end
class String
include Map::String
end
I'm looking to understand how to do this with delegate.
The Fowardable docs are hilarious--as if that first example will run without a hundred errors. Your pseudo code tells ruby to forward the method call String#map, which doesn't exist, to String#chars, and you want to join() the result of that? Skip all the method calls and just write puts "some_string". So your question doesn't seem to make a lot of sense. In any case, Forwardable#delegate() does not allow you to map one name to another name.
With regards to SimpleDelegat**or**, you can do this:
module Map
require 'delegate'
class MyStringDecorator < SimpleDelegator
def map
chars.shuffle.join('|')
end
end
end
d = Map::MyStringDecorator.new 'hello'
puts d.map
--output:--
h|o|l|l|e
Response to edit: The equivalent behavior I'm looking for..
The problem is ruby won't let you do this:
class String < SomeClass
end
which is what include does, and you need to be able to do that in order to use delegate to forward all the method calls sent to one class to another class. This is the best you can do:
require 'delegate'
class MyString < DelegateClass(String)
def map(*args, &block)
if (!args.compact.empty? || !block.nil?)
self.chars.map(*args,&block).join
else
self.chars.map(*args,&block)
end
end
end
s = MyString.new 'hello'
puts s.upcase
puts s.map {|letter| letter.succ }
--output:--
HELLO
ifmmp
Or:
require 'forwardable'
class MyString
extend Forwardable
def initialize(str)
#str = str
end
def_delegators :#str, :upcase, :capitalize, :[], :chars #etc., etc., etc.
#Or: delegate({[:upcase, :capitalize, :[], :chars] => :#str})
#Or: instance_delegate({[:upcase, :capitalize, :[], :chars] => :#str})
def map(*args, &block)
if (!args.compact.empty? || !block.nil?)
self.chars.map(*args,&block).join
else
self.chars.map(*args,&block)
end
end
end
s = MyString.new('hello')
puts s.upcase
puts s.map {|letter| letter.succ }
--output:--
HELLO
ifmmp
Of course, you could always override String#method_missing() to do what you want. What is it that you read about delegate that made you think it could replace include?

How do I add a method to an object instance with a variable from the current scope (Ruby)?

This is hard to explain as a question but here is a code fragment:
n = "Bob"
class A
end
def A.greet
puts "Hello #{n}"
end
A.greet
This piece of code does not work because n is only evaluated inside A.greet when it is called, rather than when I add the method.
Is there a way to pass the value of a local variable into A.greet?
What about if n was a function?
Use metaprogramming, specifically the define_singleton_method method. This allows you to use a block to define the method and so captures the current variables.
n = "Bob"
class A
end
A.define_singleton_method(:greet) do
puts "Hello #{n}"
end
A.greet
You can use a global ($n = "Bob")...
Although I prefer Nemo157's way you can also do this:
n = "Bob"
class A
end
#Class.instance_eval "method"
A.instance_eval "def greet; puts 'Hello #{n}' end"
#eval Class.method
eval "def A.greet2; puts 'Hi #{n}' end"
A.greet
A.greet2

Ruby call method on all following def calls?

I've seen some code that makes a class method such that you can write
class_method :instance_method,
to alias instance_method and call it from in a wrapper method every time it is called. Is there a way to be able to call class_method and have it apply to all the following definition calls (like how private works)?
I don't quite understand your question. In the future, please provide a specification of what it is exactly that you are trying to do, preferably in the form of an executable testsuite, so that we can check for ourselves whether our answers really answer your question.
Are you perhaps talking about something like this?
module MethodHook
private
def class_method(m=nil)
return if #__recursing__ # prevent infinite recursion
return #__class_method__ = true unless m
#__recursing__ = true
old_m = instance_method(m)
define_method(m) do |*args, &block|
puts "before #{m}(#{args.join(', ')})" # wrap wrap wrap
old_m.bind(self).(*args, &block)
puts "after #{m}" # more wrapping
end
#__recursing__ = nil
end
def method_added(m)
class_method(m) if #__class_method__
super
end
end
Use like this:
class Foo
extend MethodHook
def unwrapped
puts __method__
end
class_method
def wrapped
puts __method__
end
end
f = Foo.new
f.unwrapped
# unwrapped
f.wrapped
# before wrapped()
# wrapped
# after wrapped
class Foo
class_method(:unwrapped)
end
f.unwrapped
# before unwrapped()
# wrapped
# after unwrapped

Ruby method interception

I want to intercept method calls on a ruby-class and being able to do something before and after the actual execution of the method. I tried the following code, but get the error:
MethodInterception.rb:16:in before_filter': (eval):2:inalias_method': undefined method
say_hello' for classHomeWork'
(NameError)
from (eval):2:in `before_filter'
Can anybody help me to do it right?
class MethodInterception
def self.before_filter(method)
puts "before filter called"
method = method.to_s
eval_string = "
alias_method :old_#{method}, :#{method}
def #{method}(*args)
puts 'going to call former method'
old_#{method}(*args)
puts 'former method called'
end
"
puts "going to call #{eval_string}"
eval(eval_string)
puts "return"
end
end
class HomeWork < MethodInterception
before_filter(:say_hello)
def say_hello
puts "say hello"
end
end
I just came up with this:
module MethodInterception
def method_added(meth)
return unless (#intercepted_methods ||= []).include?(meth) && !#recursing
#recursing = true # protect against infinite recursion
old_meth = instance_method(meth)
define_method(meth) do |*args, &block|
puts 'before'
old_meth.bind(self).call(*args, &block)
puts 'after'
end
#recursing = nil
end
def before_filter(meth)
(#intercepted_methods ||= []) << meth
end
end
Use it like so:
class HomeWork
extend MethodInterception
before_filter(:say_hello)
def say_hello
puts "say hello"
end
end
Works:
HomeWork.new.say_hello
# before
# say hello
# after
The basic problem in your code was that you renamed the method in your before_filter method, but then in your client code, you called before_filter before the method was actually defined, thus resulting in an attempt to rename a method which doesn't exist.
The solution is simple: Don't Do That™!
Well, okay, maybe not so simple. You could simply force your clients to always call before_filter after they have defined their methods. However, that is bad API design.
So, you have to somehow arrange for your code to defer the wrapping of the method until it actually exists. And that's what I did: instead of redefining the method inside the before_filter method, I only record the fact that it is to be redefined later. Then, I do the actual redefining in the method_added hook.
There is a tiny problem in this, because if you add a method inside of method_added, then of course it will immediately get called again and add the method again, which will lead to it being called again, and so on. So, I need to guard against recursion.
Note that this solution actually also enforces an ordering on the client: while the OP's version only works if you call before_filter after defining the method, my version only works if you call it before. However, it is trivially easy to extend so that it doen't suffer from that problem.
Note also that I made some additional changes that are unrelated to the problem, but that I think are more Rubyish:
use a mixin instead of a class: inheritance is a very valuable resource in Ruby, because you can only inherit from one class. Mixins, however, are cheap: you can mix in as many as you want. Besides: can you really say that Homework IS-A MethodInterception?
use Module#define_method instead of eval: eval is evil. 'Nuff said. (There was absolutely no reason whatsoever to use eval in the first place, in the OP's code.)
use the method wrapping technique instead of alias_method: the alias_method chain technique pollutes the namespace with useless old_foo and old_bar methods. I like my namespaces clean.
I just fixed some of the limitations I mentioned above, and added a few more features, but am too lazy to rewrite my explanations, so I repost the modified version here:
module MethodInterception
def before_filter(*meths)
return #wrap_next_method = true if meths.empty?
meths.delete_if {|meth| wrap(meth) if method_defined?(meth) }
#intercepted_methods += meths
end
private
def wrap(meth)
old_meth = instance_method(meth)
define_method(meth) do |*args, &block|
puts 'before'
old_meth.bind(self).(*args, &block)
puts 'after'
end
end
def method_added(meth)
return super unless #intercepted_methods.include?(meth) || #wrap_next_method
return super if #recursing == meth
#recursing = meth # protect against infinite recursion
wrap(meth)
#recursing = nil
#wrap_next_method = false
super
end
def self.extended(klass)
klass.instance_variable_set(:#intercepted_methods, [])
klass.instance_variable_set(:#recursing, false)
klass.instance_variable_set(:#wrap_next_method, false)
end
end
class HomeWork
extend MethodInterception
def say_hello
puts 'say hello'
end
before_filter(:say_hello, :say_goodbye)
def say_goodbye
puts 'say goodbye'
end
before_filter
def say_ahh
puts 'ahh'
end
end
(h = HomeWork.new).say_hello
h.say_goodbye
h.say_ahh
Less code was changed from original. I modified only 2 line.
class MethodInterception
def self.before_filter(method)
puts "before filter called"
method = method.to_s
eval_string = "
alias_method :old_#{method}, :#{method}
def #{method}(*args)
puts 'going to call former method'
old_#{method}(*args)
puts 'former method called'
end
"
puts "going to call #{eval_string}"
class_eval(eval_string) # <= modified
puts "return"
end
end
class HomeWork < MethodInterception
def say_hello
puts "say hello"
end
before_filter(:say_hello) # <= change the called order
end
This works well.
HomeWork.new.say_hello
#=> going to call former method
#=> say hello
#=> former method called
Jörg W Mittag's solution is pretty nice. If you want something more robust (read well tested) the best resource would be the rails callbacks module.

Ruby class_eval method

I'm trying to figure out how to dynamically create methods
class MyClass
def initialize(dynamic_methods)
#arr = Array.new(dynamic_methods)
#arr.each { |m|
self.class.class_eval do
def m(*value)
puts value
end
end
}
end
end
tmp = MyClass.new ['method1', 'method2', 'method3']
Unfortunately this only creates the method m but I need to create methods based on the value of m, ideas?
There are two accepted ways:
Use define_method:
#arr.each do |method|
self.class.class_eval do
define_method method do |*arguments|
puts arguments
end
end
end
Use class_eval with a string argument:
#arr.each do |method|
self.class.class_eval <<-EVAL
def #{method}(*arguments)
puts arguments
end
EVAL
end
The first option converts a closure to a method, the second option evaluates a string (heredoc) and uses regular method binding. The second option has a very slight performance advantage when invoking the methods. The first option is (arguably) a little more readable.
define_method(m) do |*values|
puts value
end

Resources