Yield/block propagation

Well, rubyMEL is now a reality for the past couple of days. I have to admit I
was blown away at how well it blends with maya and how easy it was to get a
first prototype going.
Also some of ruby’s unique features (like the ability to re-define Kernel::` is
now helping to make blending the two languages even more seamlessly). It’s
also working really nicely with FOX (albeit Tk has proven not doable so far).
This will help bring Ruby into the radar of the film/vfx and 3d community in
general.

http://www.highend3d.com/maya/plugins/?section=utilities#2838

···

Anyway, I’m doing minor cleanups in the code and building up now a library of
Ruby code to OO mel window’s toolkit.

I have some code like:

def rowLayout(w = nil, *args)
rowLayout
if ! block_given?
raise "layout needs a block"
end

if ( w )
yield w
else
yield
setParent ..
end
end

which repeats on several places, and I would like to move the whole section
dealing with the block and yield into a separate function. However, I am
unsure if it is possible to have ruby propagate the block from one function to
another and to have it yield not to the previous call but two functions down.
That is, I want:

def layout(w = nil, *args)
if ! block_given?
raise "layout needs a block"
end

if ( w )
yield w
else
yield
setParent ..
end
end

def rowLayout(w = nil, *args)
rowLayout
layout(w,args)
end


I might miss what you are wanting to do… but are you looking for the
"&" trick for collecting the block, so you can pass it on?

[ensemble] ~/p/ruby/vcard $ irb
irb(main):001:0> def foo; yield “hello, world”; end
nil
irb(main):002:0> def bar(&proc); puts ‘"’; foo &proc; puts ‘"’; end
nil
irb(main):003:0> bar { |h| puts “#{h} #{h}” }
"
hello, world hello, world
"
nil

Cheers,
Sam

def layout(w = nil, *args)

well, I've not understood. This ?

svg% cat b.rb
#!/usr/bin/ruby

def a
   yield 12
end

def b(&block)
   a(&block)
end

b {|i| p i}
b
svg%

svg% b.rb
12
./b.rb:4:in `a': no block given (LocalJumpError)
        from ./b.rb:8:in `b'
        from ./b.rb:12
svg%

Guy Decoux

well, I’ve not understood. This ?

Hmm… yes. I was not sure what syntax should a block be passed with,
specially when using optional arguments.
That clears it out.