Frage

w = Gtk::window.new
w do
   title = "foo"
   resizable = false
   foo = bar
end

I want to call every method in the block to self after passing it to the block.

-thanks.

War es hilfreich?

Lösung

I believe one way to do that could be usage of #instance_eval like:

a = 'asd'
a.instance_eval do
  a.upcase!
end

a # => ASD

Other way to get an object inside a block is by using #tap, but you will not have self:

gkt_window = Gtk::window.new

gtk_window.tap do |w|
  w.title     = 'foo'
  w.resizable = false
  w.foo       = bar
end # Now gtk_window will have all properties set and you can chain methods to this block

Andere Tipps

You can use BasicObject#instance_eval:

w.instance_eval do
  title = 'foo'
  resizable = false
  foo = bar
end
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top