# How to return more than one object to a block

**URL:** <https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870>\
**Category:** Ruby\
**Created:** [April 25, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870 "2008-04-25T20:43:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [April 25, 2008, 8:43pm UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870/1 "2008-04-25T20:43:02Z")

</div>

Hi,

I’m kinda new to blocks, and I’m trying to create my own Rails form  
builder, which I want to work like this:

form\_for … do |f|  
f.form\_label ‘Some label’ do  
f.text\_field :something  
link\_to ‘somewhere’, somewhere\_path  
end  
end

In my form\_label method I capture the block output and wrap some HTML  
around it, but the problem is that the block only captures the last  
statement, in this case, the link\_to.

Here’s a more clean explanation:

def dothis  
yield  
end

x = dothis() {  
‘this’  
‘that’  
}

How do I make ‘x’ be an array holding both the strings. Of course I can  
just make those strings into an array, but I want to do it from the  
dothis method.

What do you know… After writing this, something occurred to me:

def dothis  
yield([]).join("\n")  
end

dothis {|a|  
a \<\< ‘this’  
a \<\< ‘that’  
}

So that works. Would this be the best approach?

Thanks!

- Ivan

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [April 25, 2008, 10:54pm UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870/2 "2008-04-25T22:54:57Z")

</div>

On Apr 25, 2008, at 20:43, Ivan V. wrote:

> How do I make ‘x’ be an array holding both the strings. Of course I  
> can  
> just make those strings into an array, but I want to do it from the  
> dothis method.

x = dothis() {  
[‘this’, ‘that’]  
}

should do it.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [April 25, 2008, 11:24pm UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870/3 "2008-04-25T23:24:59Z")

</div>

On Apr 25, 2008, at 22:54, Mikael Høilund wrote:

> > ‘this’  
> > }
> 
> should do it.

Sorry, I seem to have mis-read your initial question. In order to do  
what you asked, you would have to make them an array, though (unless  
some of the plans for Ruby 1.9 are realized, which I sincerely hope  
they are not). Maybe if you give a use-case, I could be a little more  
helpful, though.

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [April 25, 2008, 11:48pm UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870/4 "2008-04-25T23:48:54Z")

</div>

Mikael HÃ¸ilund wrote:

> On Apr 25, 2008, at 22:54, Mikael Hï¿½ilund wrote:
> 
> > > ‘this’  
> > > }
> > 
> > should do it.
> 
> Sorry, I seem to have mis-read your initial question. In order to do  
> what you asked, you would have to make them an array, though (unless  
> some of the plans for Ruby 1.9 are realized, which I sincerely hope  
> they are not). Maybe if you give a use-case, I could be a little more  
> helpful, though.

Hi,

My use case is like I stated on the start of my message. I created a  
function that takes a block, transforms the output of what’s inside the  
block, and outputs the result. I came up with something like this:

def form\_label(label)  
block = yield([])  
block = block.join("\n") if block.is\_a?(Array)  
“#{label}: #{block}”  
end

form\_label ‘example’ do |a|  
a \<\< text\_field\_tag …  
a \<\< link\_to …  
a \<\< ‘something more’  
end

I didn’t want to do something special inside the block (more than just  
calling functions, or returning strings), as I thought “yield” would  
capture everything that’s inside it… which doesn’t make sense once I  
think more about it 🙂

- Ivan

---

<div class="post-metadata">

**Author:** ![darth\_ivan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/22d042/32.png) [@darth\_ivan](https://www.ruby-forum.com/u/darth_ivan)\
**Post date:** [April 26, 2008, 11:35am UTC](https://www.ruby-forum.com/t/how-to-return-more-than-one-object-to-a-block/135870/5 "2008-04-26T11:35:37Z")

</div>

- Ivan V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (20:43) schrieb:

> I’m kinda new to blocks, and I’m trying to create my own Rails form  
> builder, which I want to work like this:
> 
> form\_for … do |f|  
> f.form\_label ‘Some label’ do  
> f.text\_field :something  
> link\_to ‘somewhere’, somewhere\_path  
> end  
> end

Normally you pass that a parameter that represents the form\_label, so  
that the block can do something with it. Just like the form block does,  
it’s passed f, which represents the form.

You better Rails stuff on some Rails list.

mfg, simon … l
