SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Continente de São Pedro
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Blocks and context

    Hi,

    When learning ruby blocks I've read that the context of the block, as the example in http://www.rubycentral.com/book/tut_containers.html
    Code:
    def nTimes(aThing)
      return proc { |n| aThing * n }
    end
    p1 = nTimes(23)
    p1.call(3) 	» 	69
    p1.call(4) 	» 	92
    p2 = nTimes("Hello ")
    p2.call(3) 	» 	"Hello Hello Hello "
    But now I wanna to write GUIs and reading the QtRuby documentation I saw this use of blocks:
    Code:
     w = MyWidget.new { setCaption("foobar") }
    . The document explains the sample saying that " The block will be called in the context of the newly created instance". Is it a misuse of ruby blocks??

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think so. If you didn't use blocks for that you would have this code:

    Code:
    w = MyWidget.new
    w.setCaption("foobar")
    And you couln't pass it as a parameter then:

    Code:
    w = MyWidget.new
    w.setCaption("foobar")
    foo(w)
    vs.

    Code:
    foo(MyWidget.new {
                      setCaption("foobar")
    })

  3. #3
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Continente de São Pedro
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It could be done with blocks like this
    Code:
    w = MyWidget.new { |widget| widget.setCaption("foobar") }
    w/a change the context into the new instance. This change on context doesn't break the concept of closure??

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Normally blocks are executed in a definition (caller) context
    Code:
    class A
    	def initialize
    		yield
    	end
    end
    A.new { p self } #main
    With instance_eval it's possible to execute block in callee's (or any other) context:
    Code:
    class B
    	def initialize(&block)
    		instance_eval(&block)
    	end
    end
    B.new { p self } #B...
    and this is indeed a method tk widgets use:
    Code:
    require 'tk'
    TkLabel.new { p self } # TkLabel...
    I don't know if that is a good or bad "practice", but at the first glance it looks confusing. For example, you're unable to use caller's methods inside the block:
    Code:
    class X
    	def foo
    		p "foo"
    	end
    	def bar
    		TkLabel.new { foo() } #doesn't work!
    	end
    end

  5. #5
    SitePoint Member
    Join Date
    Jan 2004
    Location
    Continente de São Pedro
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the answer stereofrog ... I was wondering wether to use this "approach" in my code or not, but I agree it's confusing, so I will keep it away


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •