Joel VanderWerf wrote: > shasckaw wrote: > >> Hello, >> when using tk, you can initialize tk objects with a block, example: >> >> TkLabel.new(root) { >> text 'Hello, World!' >> pack { padx 15 ; pady 15; side 'left' } >> } > > > Here's how Tk might do it, though I haven't studied the source. (I don't > think there's any way around using #instance_eval.) > > [code sample] Great, it works! I have modified my own test this way: class Dialect def initialize(&block) instance_eval(&block) if block end def add_some_stuff(stuff) @stuff = stuff end def show p @stuff end end Dialect.new {add_some_stuff("Hello world!"); show} And it works. > > There are some drawbacks: > > - inside the block, all private methods and instance variables are > accessible I have found a workaround: class Dialect class Local def add_some_stuff(stuff) @stuff = stuff end def show p @stuff end end def initialize(&block) Local.new.instance_eval(&block) if block end end Dialect.new {add_some_stuff("Hello world!"); show} > > - you can't access from within the block attributes that are defined in > the context outside the block: > > @x = 1 > pack { > padx @x # this is a different @x > } > > instead, however, you can use local variables to "pass in" the value: > > temp_x = @x = 1 > pack { > padx temp_x > } After some tests, I've realised that it is the same case for Tk code. Here is a sample: require 'tk' class Atest def initialize @vartest = "Try this" root = TkRoot.new { title "Ex1" } TkLabel.new(root) { text "|" + @vartest.to_s + "|" pack { padx 15 ; pady 15; side 'left' } } end end Atest.new Tk.mainloop I've put a to_s because it crashes with '+' not working on nil. Until Rite adds those macro, I'll make do with this technic. Thanks again for your help. Shasckaw