How to return more than one object to a block

I

Ivan V.

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
 
M

Mikael Høilund

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.

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

should do it.
 
M

Mikael Høilund

Here's a more clean explanation:

def dothis
yield
end

x =3D dothis() {
'this'
'that'
}

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

x =3D dothis() {
['this', 'that']
}

should do it.

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

helpful, though.=
 
I

Ivan V.

Mikael said:
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
 
S

Simon Krahnke

* Ivan V. said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top