On Sun, Mar 29, 2009 at 7:28 AM, Charles Oliver Nutter
<charles.nutter / sun.com> wrote:
> Thanks! I know the instance_eval'ed pattern for executing blocks is
> sometimes considered bad form, but it makes the code look so nice here I
> figured I'd go for it. If you want to use it as a normal API, you can do
> that just as easily; you just have to "start" and "stop" the method body
> yourself:
>
> fb =3D FileBuilder.build(__FILE__) do
> cls =3D fb.public_class "SimpleLoop"
> m =3D cls.public_static_method "main", void, string[]
> m.start
> m.aload 0
> m.push_int 0
> m.aaload
> m.label :top
> m.dup
> m.aprintln
> m.goto :top
> m.returnvoid
> m.stop

Charlie, you can also use some 'trick' to allow programmers choose if
he wants instance_eval or 'normal' version:

class Foo
  def initialize(*args, &block)
    if block_given?
      if block.arity =3D=3D 1
        block.call(self)
      else
        instance_eval(&block)
      end
    end
  end
end

Foo.new do |foo|
  puts "self =3D #{self}"
  puts "foo =3D #{foo}"
end

Foo.new do
  puts "self =3D #{self}"
end

--=20
Pozdrawiam

Rados=B3aw Bu=B3at
http://radarek.jogger.pl - m=F3j blog