From: Samuel Williams Date: 2012-07-17T01:19:34+09:00 Subject: Re: Providing a object-specific class scope? --047d7b66f54d80bfcc04c4f4cbfe Content-Type: text/plain; charset=ISO-8859-1 Clearly I shouldn't be coding at 4am. The above example doesn't actually work correctly =) I'm going to shut up and go to bed now. On 17 July 2012 03:53, Samuel Williams wrote: > 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 >> > > --047d7b66f54d80bfcc04c4f4cbfe Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Clearly I shouldn't be coding at 4am. The above example doesn't act= ually work correctly =3D) I'm going to shut up and go to bed now.
On 17 July 2012 03:53, Samuel Williams <space.ship.traveller@gmail.com> wrote:
I hate to answer my own qu= estion, but after some more experimentation I found that this works pretty = well:

#!/usr/bin/env ruby

class Transacti= on
def scope(helper= s)
helpers.class_e= val do
Proc.new {}
= end
end
end

<= div> module Helpers
<= span style=3D"white-space:pre-wrap"> def self.hello_world
puts "Hello World"
end
end

transaction =3D Transaction.new

eval "hello_world", tran= saction.scope(Helpers).binding

I hope this may be u= seful to others.

Kind regards,
Samuel
On 17 July 2012 0= 3:35, Samuel Williams <space.ship.traveller@gmail.com>= wrote:
Is it= 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


--047d7b66f54d80bfcc04c4f4cbfe--