From: Ilya Lopatkin Date: 2010-02-04T19:33:18+09:00 Subject: Where is the magic? I'm learning Ruby and have found some interesting behaviour which I cann't understand myself yet. Here's some code and the output: class Foo def self.new(*args) obj = super yield obj end end Foo.new do p self end Struct.new("Foo", :foo, :bar) do p self end => main => Struct::Foo The first output is quite understandable because self is bounded to the top-level class of the file and is sent to the closure. But in the second line it seems that scope of self has been changed somehow. I've had a fast look on the source file struct.c and haven't found something special in the new method. The same thing can also be found in the QtRuby bindings: require 'Qt4' Qt::Application.new(ARGV) do Qt::Widget.new do self.window_title = 'Hello QtRuby v1.0' resize(200, 100) button = Qt::PushButton.new('Quit') do connect(SIGNAL :clicked) { Qt::Application.instance.quit } end label = Qt::Label.new('Hello Qt in the Ruby way!') self.layout = Qt::VBoxLayout.new do add_widget(label, 0, Qt::AlignCenter) add_widget(button, 0, Qt::AlignRight) end show end exec end Please, could anybody explain me how does this work. Links will do too. :) -- Posted via http://www.ruby-forum.com/.