Can I set a module context for a block?

J

John Lam

------=_Part_39612_19483744.1133795851458
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'd really like to be able to inject the 'include Bob' statement in the
create method of Bob:

module Bob
def method_missing(name, *params)
puts "missing: #{name}"
end
def self.create(name)
yield if block_given?
puts name
end
end

Bob::create('hi') do
include Bob
ldstr
ldc_i4_0
end

Is there a way that I can avoid include Bob in my block?

Thanks
-John
http://www.iunknown.com

------=_Part_39612_19483744.1133795851458--
 
R

Robert Klemme

John said:
I'd really like to be able to inject the 'include Bob' statement in
the create method of Bob:

module Bob
def method_missing(name, *params)
puts "missing: #{name}"
end
def self.create(name)
yield if block_given?
puts name
end
end

Bob::create('hi') do
include Bob
ldstr
ldc_i4_0
end

Is there a way that I can avoid include Bob in my block?

module Bob
def method_missing(name, *params)
puts "missing: #{name}"
end
def self.create(name,&b)
obj = Object.new
obj.extend Bob
obj.instance_eval(&b)
puts name
end
end

Bob::create('hi') do
ldstr
ldc_i4_0
end

irb(main):001:0> module Bob
irb(main):002:1> def method_missing(name, *params)
irb(main):003:2> puts "missing: #{name}"
irb(main):004:2> end
irb(main):005:1> def self.create(name,&b)
irb(main):006:2> obj = Object.new
irb(main):007:2> obj.extend Bob
irb(main):008:2> obj.instance_eval(&b)
irb(main):009:2> puts name
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0>
irb(main):013:0* Bob::create('hi') do
irb(main):014:1* ldstr
irb(main):015:1> ldc_i4_0
irb(main):016:1> end
missing: ldstr
missing: ldc_i4_0
hi
=> nil

Kind regards

robert
 
N

Neil Stevens

John said:
I'd really like to be able to inject the 'include Bob' statement in the
create method of Bob:

module Bob
def method_missing(name, *params)
puts "missing: #{name}"
end
def self.create(name)
yield if block_given?
puts name
end
end

Bob::create('hi') do
include Bob
ldstr
ldc_i4_0
end

Is there a way that I can avoid include Bob in my block?

def self.create(name)
module_eval do
yield
end if block_given?
puts name
end
 
N

Neil Stevens

Neil Stevens wrote:
[something wrong]

Bah, never mind, I thought I'd removed the 'include Bob' when I tested,
but I hadn't, heh.
 
J

John Lam

------=_Part_39936_23151554.1133796889588
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Thanks, Robert! Your solution neatly solves my next problem, which was how
to declare a private context for the evaluation of the method_missing
elements.

Cheers,
-John
http://www.iunknown.com


module Bob
def method_missing(name, *params)
puts "missing: #{name}"
end
def self.create(name,&b)
obj =3D Object.new
obj.extend Bob
obj.instance_eval(&b)
puts name
end
end

------=_Part_39936_23151554.1133796889588--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top