Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically replace method implementation on an object in Ruby

I'd like to replace the implementation of a method for an object with a block that the user specifies. In JavaScript, this is easily accomplished:

function Foo() {
    this.bar = function(x) { console.log(x) }
}
foo = new Foo()
foo.bar("baz")
foo.bar = function(x) { console.error(x) }
foo.bar("baz")

In C# it is also quite easy

class Foo
{
    public Action<string> Bar { get; set; }
    public Foo()
    {
        Bar = x => Console.WriteLine(x);
    }
}

var foo = Foo.new();
foo.Bar("baz");
foo.Bar = x => Console.Error.WriteLine(x);
foo.Bar("baz");

But how can I do the same in Ruby? I have a solution that stores a lambda in an instance variable and the method calls the lambda, but I don't really like the overhead and syntax

class Foo
  def initialize
    @bar = lambda {|x| puts x}
  end

  def bar x
    @bar.call x
  end

  def bar= blk
    @bar = blk
  end
end

foo = Foo.new
foo.bar "baz"
foo.bar= lambda {|x| puts "*" + x.to_s}
foo.bar "baz"

I'd like to have a syntax like that:

foo.bar do |x|
    puts "*" + x.to_s
end
foo.bar "baz"

I came up with the following code

class Foo
  def bar x = nil, &blk
    if (block_given?)
      @bar = blk
    elsif (@bar.nil?)
      puts x
    else
      @bar.call x
    end
  end
end

But this is kinda ugly for more than one parameter and still doesn't feel 'right'. I could also define a set_bar method, but i don't like that either :).

class Foo
  def bar x
    if (@bar.nil?)
      puts x
    else
      @bar.call x
    end
  end

  def set_bar &blk
    @bar = blk
  end
end

So question is: Is there a better way do to do this and if not, what way would you prefer

Edit: @welldan97's approach works, but i loose the local variable scope, i.e.

prefix = "*"
def foo.bar x
    puts prefix + x.to_s
end

doesn't work. I suppose I have to stick with lambda for that to work?

like image 521
kev Avatar asked Mar 20 '12 12:03

kev


People also ask

How do you dynamically define a method in Ruby?

One way to invoke a method dynamically in ruby is to send a message to the object. We can send a message to a class either within the class definition itself, or by simply sending it to the class object like you'd send any other message. This can be accomplished by usin send.

What is Metaclass in Ruby?

Class method frontend that we defined earlier is nothing but an instance method defined in the metaclass for the object Developer ! A metaclass is essentially a class that Ruby creates and inserts into the inheritance hierarchy to hold class methods, thus not interfering with instances that are created from the class.

What is MetaProgramming in Ruby?

Metaprogramming is a technique in which code operates on code rather than on data. It can be used to write programs that write code dynamically at run time. MetaProgramming gives Ruby the ability to open and modify classes, create methods on the fly and much more.

What is Def and end in Ruby?

In Ruby, we call it a method. Before we can use a method, we must first define it with the reserved word def . After the def we give our method a name. At the end of our method definition, we use the reserved word end to denote its completion.


2 Answers

use def:

foo = Foo.new
foo.bar "baz"

def foo.bar x
  puts "*" + x.to_s
end

foo.bar "baz"

yes, that simple

Edit: To not loose the scope you can use define_singleton_method(as in @freemanoid answer):

 prefix = "*"

 foo.define_singleton_method(:bar) do |x|
   puts prefix + x.to_s
 end

 foo.bar 'baz'
like image 167
welldan97 Avatar answered Sep 29 '22 10:09

welldan97


You can implement what you want like this:

class Foo; end

foo = Foo.new
prefix = '*'
foo.send(:define_singleton_method, :bar, proc { |x| puts prefix + x.to_s })
foo.bar('baz')
"*baz" <<<<<-- output

This is absolutely normal and correct in ruby.

like image 39
freemanoid Avatar answered Sep 29 '22 11:09

freemanoid