[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

A Ruby block question

David Trasbo

10/16/2008 11:21:00 AM

I am in need of making a method that accepts a block. It basicly needs
to do the following:

* The method it self should output some content first.
* Then after that content it should output the content defined in
&block.
* And at last output some other content defined by the method it self.

In other words I need to put user-defined input in between some
pre-defined content. How can I approach that?
--
Posted via http://www.ruby-....

22 Answers

Stefano Crocco

10/16/2008 11:24:00 AM

0

Alle Thursday 16 October 2008, David Trasbo ha scritto:
> I am in need of making a method that accepts a block. It basicly needs
> to do the following:
>
> * The method it self should output some content first.
> * Then after that content it should output the content defined in
> &block.
> * And at last output some other content defined by the method it self.
>
> In other words I need to put user-defined input in between some
> pre-defined content. How can I approach that?

If I understand correctly what you want to do, you don't need to do anything
special:

def my_method
puts "predefined text 1"
puts yield
puts "predefined text 2"
end

Stefano


Nathan Powell

10/16/2008 11:27:00 AM

0

On Thu, Oct 16, 2008 at 08:20:34PM +0900, David Trasbo wrote:
> I am in need of making a method that accepts a block. It basicly needs
> to do the following:
>
> * The method it self should output some content first.
> * Then after that content it should output the content defined in
> &block.
> * And at last output some other content defined by the method it self.
>
> In other words I need to put user-defined input in between some
> pre-defined content. How can I approach that?

Look at yield?


def foo(&block)
puts "Oh Hai"
yield
puts "kthxbai"
end

fooi {puts "No Wai!"}

--
nathan
nathan_at_nathanpowell_dot_org

Another flaw in the human character is that everybody wants to build
and nobody wants to do maintenance.
~ Kurt Vonnegut
------------------------------------

David Trasbo

10/16/2008 11:34:00 AM

0

Stefano Crocco wrote:

>> In other words I need to put user-defined input in between some
>> pre-defined content. How can I approach that?
>
> If I understand correctly what you want to do, you don't need to do
> anything
> special:
>
> def my_method
> puts "predefined text 1"
> puts yield
> puts "predefined text 2"
> end

My method looks like this:

def fields_for_setting(namespace, name, &block)
namespace = namespace.to_s
name = name.to_s
id = "settings_#{namespace}_#{name}_"
m = Builder::XmlMarkup.new :indent => 2
fields_for "settings[]", setting =
Setting.find_or_initialize_by_namespace_and_name(namespace, name) do |f|
m.p do
unless setting.new_record?
m << (f.hidden_field :id, :index => nil, :id => id+"id")
end
m << (f.hidden_field :namespace, :index => nil, :id =>
id+"namespace")
m << (f.hidden_field :name, :index => nil, :id => id+"name")
#puts yield f???
end
end
end

The problem is, I'm using Builder::XmlMarkup and the fields_for from
Rails. I want to put some hidden fields AND the text fields defined by
the block into a <p> tag. That means I need to be able to access the f
object provided by fields_for and use it in this method's block.

But how?
--
Posted via http://www.ruby-....

Stefano Crocco

10/16/2008 11:47:00 AM

0

Alle Thursday 16 October 2008, David Trasbo ha scritto:
> Stefano Crocco wrote:
> >> In other words I need to put user-defined input in between some
> >> pre-defined content. How can I approach that?
> >
> > If I understand correctly what you want to do, you don't need to do
> > anything
> > special:
> >
> > def my_method
> > puts "predefined text 1"
> > puts yield
> > puts "predefined text 2"
> > end
>
> My method looks like this:
>
> def fields_for_setting(namespace, name, &block)
> namespace = namespace.to_s
> name = name.to_s
> id = "settings_#{namespace}_#{name}_"
> m = Builder::XmlMarkup.new :indent => 2
> fields_for "settings[]", setting =
> Setting.find_or_initialize_by_namespace_and_name(namespace, name) do |f|
> m.p do
> unless setting.new_record?
> m << (f.hidden_field :id, :index => nil, :id => id+"id")
> end
> m << (f.hidden_field :namespace, :index => nil, :id =>
> id+"namespace")
> m << (f.hidden_field :name, :index => nil, :id => id+"name")
> #puts yield f???
> end
> end
> end
>
> The problem is, I'm using Builder::XmlMarkup and the fields_for from
> Rails. I want to put some hidden fields AND the text fields defined by
> the block into a <p> tag. That means I need to be able to access the f
> object provided by fields_for and use it in this method's block.
>
> But how?

I don't know Rails and Builder::XmlMarkup, so I can be completely wrong. From
a quick look at Builder::XmlMarkup api, I'd say that, if the block returns the
string you want to insert in the p element, you only have to insert it in m
like you did for the texts:

m << yield f

Stefano

David Trasbo

10/16/2008 11:55:00 AM

0

Stefano Crocco wrote:

>> But how?
>
> I don't know Rails and Builder::XmlMarkup, so I can be completely wrong.
> From
> a quick look at Builder::XmlMarkup api, I'd say that, if the block
> returns the
> string you want to insert in the p element, you only have to insert it
> in m
> like you did for the texts:
>
> m << yield f

Yes, that was also my first idea, but that is giving me this syntax
error:

syntax error, unexpected tIDENTIFIER, expecting kEND
--
Posted via http://www.ruby-....

David A. Black

10/16/2008 11:58:00 AM

0

Hi --

On Thu, 16 Oct 2008, David Trasbo wrote:

> Stefano Crocco wrote:
>
>>> But how?
>>
>> I don't know Rails and Builder::XmlMarkup, so I can be completely wrong.
>> From
>> a quick look at Builder::XmlMarkup api, I'd say that, if the block
>> returns the
>> string you want to insert in the p element, you only have to insert it
>> in m
>> like you did for the texts:
>>
>> m << yield f
>
> Yes, that was also my first idea, but that is giving me this syntax
> error:
>
> syntax error, unexpected tIDENTIFIER, expecting kEND

Put f in parens:

m << yield(f)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

David Trasbo

10/16/2008 11:58:00 AM

0

David Trasbo wrote:

> syntax error, unexpected tIDENTIFIER, expecting kEND

Then I tried this:

m << (yield f)

But that is giving me this error:

can't convert nil into String

That doesn't really make sence, does it?
--
Posted via http://www.ruby-....

David A. Black

10/16/2008 12:04:00 PM

0

Hi --

On Thu, 16 Oct 2008, David Trasbo wrote:

> David Trasbo wrote:
>
>> syntax error, unexpected tIDENTIFIER, expecting kEND
>
> Then I tried this:
>
> m << (yield f)
>
> But that is giving me this error:
>
> can't convert nil into String
>
> That doesn't really make sence, does it?

Actually it does, because your block contains a call to puts, and puts
always returns nil. You probably want your block just to contain a
string.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!

David Trasbo

10/16/2008 12:06:00 PM

0

David A. Black wrote:

>>> m << yield f
>>
>> Yes, that was also my first idea, but that is giving me this syntax
>> error:
>>
>> syntax error, unexpected tIDENTIFIER, expecting kEND
>
> Put f in parens:
>
> m << yield(f)

That's also a possibility. But it seems to be a little more complicated
as mentioned.

can't convert nil into String
--
Posted via http://www.ruby-....

David A. Black

10/16/2008 12:10:00 PM

0

On Thu, 16 Oct 2008, David Trasbo wrote:

> David A. Black wrote:
>
>>>> m << yield f
>>>
>>> Yes, that was also my first idea, but that is giving me this syntax
>>> error:
>>>
>>> syntax error, unexpected tIDENTIFIER, expecting kEND
>>
>> Put f in parens:
>>
>> m << yield(f)
>
> That's also a possibility. But it seems to be a little more complicated
> as mentioned.
>
> can't convert nil into String

You're one post behind -- read my previous answer :-)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.r... for details and updates!