From: Samuel Williams Date: 2012-07-17T00:53:48+09:00 Subject: Re: Providing a object-specific class scope? --047d7b3a7ff84ab03204c4f46f53 Content-Type: text/plain; charset=ISO-8859-1 I hate to answer my own question, but after some more experimentation I found that this works pretty well: #!/usr/bin/env ruby class Transaction def scope(helpers) helpers.class_eval do Proc.new {} end end end module Helpers def self.hello_world puts "Hello World" end end transaction = Transaction.new eval "hello_world", transaction.scope(Helpers).binding I hope this may be useful to others. Kind regards, Samuel On 17 July 2012 03:35, Samuel Williams wrote: > Is it possible to do something like this with Ruby? > > #!/usr/bin/env ruby > > class Scope > def hello_world > puts "Hello World" > end > end > > scope = Scope.new > > transaction = class << scope; self; end; > > # Without making Scope#hello_world a class method, can we make this work > somehow? > eval "hello_world", transaction.send(:binding) > > The idea would be to provide the ability to write something along the > lines of transaction.send(:include, MyModule) without affecting the > original instance of scope, while still providing access to all methods. > > Kind regards, > Samuel > --047d7b3a7ff84ab03204c4f46f53 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable I hate to answer my own question, but after some more experimentation I fou= nd that this works pretty well:

#!/usr/bin/env ruby

class Transaction
def scope(helpers)
helpers.class_eval do
Pr= oc.new {}
end
end
end

module Helpers
def self.hello_world
put= s "Hello World"
end
end

transaction =3D Transaction.new

eval "hello_w= orld", transaction.scope(Helpers).binding

I hope this may be useful to others.

Kind regards,
Samuel

On 17 July 2012 03:35, Samuel Williams <space.ship.= traveller@gmail.com> wrote:
Is i= t possible to do something like this with Ruby?

#!/usr/bin/env ruby

class Scop= e
def hello_world
puts "Hello World&quo= t;
end
end

<= div> scope =3D Scope.new
<= div>
transaction= =3D class << scope; self; end;

# Without = making Scope#hello_world a class method, can we make this work somehow?
eval "hello_world&= quot;, transaction.send(:binding)

The idea would be to provide the ability to write= something along the lines of transaction.send(:include, MyModule) without = affecting the original instance of scope, while still providing access to a= ll methods.

Kind regards,
Samuel

--047d7b3a7ff84ab03204c4f46f53--