Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > A Ruby block question

Reply
Thread Tools

A Ruby block question

 
 
David Trasbo
Guest
Posts: n/a
 
      10-16-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      10-16-2008
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


 
Reply With Quote
 
 
 
 
Nathan Powell
Guest
Posts: n/a
 
      10-16-2008
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
------------------------------------

 
Reply With Quote
 
David Trasbo
Guest
Posts: n/a
 
      10-16-2008
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(n amespace, 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-forum.com/.

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      10-16-2008
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(n amespace, 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

 
Reply With Quote
 
David Trasbo
Guest
Posts: n/a
 
      10-16-2008
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-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      10-16-2008
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.rubypal.com for details and updates!

 
Reply With Quote
 
David Trasbo
Guest
Posts: n/a
 
      10-16-2008
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-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      10-16-2008
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.rubypal.com for details and updates!

 
Reply With Quote
 
David Trasbo
Guest
Posts: n/a
 
      10-16-2008
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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Noob Q: ruby block scoping question (ruby TK) Philip Amadeo Saeli Ruby 4 05-01-2008 07:43 PM
Fo:Block can you check to see if a block contains any text by using the block id? morrell XML 1 10-10-2006 07:18 PM
Problem with enterprise application block - data block Showjumper ASP .Net 1 03-19-2005 03:48 PM
Block DIV within a block DIV? Noozer HTML 3 01-06-2005 10:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57