[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Proc multiple returns?

Ryan Lewis

5/30/2008 11:23:00 AM

code:
def foo(&block)
block.call if block
end

p foo {
"bar"
"baz"
}

=> "baz"

Now, any ideas on how to make it return
=> ["bar", "baz"]

I've been tryin all night -_-
--
Posted via http://www.ruby-....

12 Answers

David A. Black

5/30/2008 11:29:00 AM

0

Hi --

On Fri, 30 May 2008, Ryan Lewis wrote:

> code:
> def foo(&block)
> block.call if block
> end
>
> p foo {
> "bar"
> "baz"
> }
>
> => "baz"
>
> Now, any ideas on how to make it return
> => ["bar", "baz"]
>
> I've been tryin all night -_-

def foo
yield if block_given? # no point doing it the slow way :-)
end

p foo { ["bar", "baz"] }

I have a feeling there may be something more to your question that I'm
not seeing.


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

David A. Black

5/30/2008 11:38:00 AM

0

On Fri, 30 May 2008, David A. Black wrote:

> Hi --
>
> On Fri, 30 May 2008, Ryan Lewis wrote:
>
>> code:
>> def foo(&block)
>> block.call if block
>> end
>>
>> p foo {
>> "bar"
>> "baz"
>> }
>>
>> => "baz"
>>
>> Now, any ideas on how to make it return
>> => ["bar", "baz"]
>>
>> I've been tryin all night -_-
>
> def foo
> yield if block_given? # no point doing it the slow way :-)
> end
>
> p foo { ["bar", "baz"] }
>
> I have a feeling there may be something more to your question that I'm
> not seeing.

Maybe this:

foo { break "bar", "baz" }

?


David

--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
See http://www.r... for details and updates!

Andreas Warberg

5/30/2008 12:18:00 PM

0

Ryan Lewis wrote:
> code:
> def foo(&block)
> block.call if block
> end
>
> p foo {
> "bar"
> "baz"
> }
>
> => "baz"
>
> Now, any ideas on how to make it return
> => ["bar", "baz"]

How about:

irb(main):010:0> def foo(&block); block.call if block; end
=> nil
irb(main):011:0> foo { ["bar", "baz"]}
=> ["bar", "baz"]
irb(main):012:0>

And you could explode it to multiple variables like this:
irb(main):021:0> a,b = *foo { ["bar", "baz"]}
=> ["bar", "baz"]
irb(main):022:0> a
=> "bar"
irb(main):023:0> b
=> "baz"
irb(main):024:0>

Andreas
--
Posted via http://www.ruby-....

Ryan Lewis

5/30/2008 12:23:00 PM

0

Andreas Warberg wrote:
>...

You dont understand, I'm making a simple HTML module for easy document
creating.
The code will look like this:
HTML::body {
"blahblah"
HTML::h1 {
"zomg headers"
}
}

Assuming HTML::h1 returns "<h1>zomg headers</h1>", HTML::body will only
return "<h1>zomg headers</h1>".
But I need it to return ["blahblah", "<h1>zomg headers</h1>"]
--
Posted via http://www.ruby-....

Andreas Warberg

5/30/2008 12:32:00 PM

0

Ryan Lewis wrote:
> The code will look like this:
> HTML::body {
> "blahblah"
> HTML::h1 {
> "zomg headers"
> }
> }
>
> Assuming HTML::h1 returns "<h1>zomg headers</h1>", HTML::body will only
> return "<h1>zomg headers</h1>".
> But I need it to return ["blahblah", "<h1>zomg headers</h1>"]

Maybe you should look into Ruby CGI. Its a part of the standard library:
http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/...

Here is a good introduction:
http://coolnamehere.com/geekery/ruby/we...

Andreas
--
Posted via http://www.ruby-....

Axel Etzold

5/30/2008 12:34:00 PM

0


-------- Original-Nachricht --------
> Datum: Fri, 30 May 2008 21:22:33 +0900
> Von: Ryan Lewis <c00lryguy@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: Re: Proc multiple returns?

> Andreas Warberg wrote:
> >...

Dear Ryan,

>
> You dont understand, I'm making a simple HTML module for easy document
> creating.
> The code will look like this:
> HTML::body {
> "blahblah"
> HTML::h1 {
> "zomg headers"
> }
> }
>
> Assuming HTML::h1 returns "<h1>zomg headers</h1>", HTML::body will only
> return "<h1>zomg headers</h1>".
> But I need it to return ["blahblah", "<h1>zomg headers</h1>"]

The function will always return the last statement - you could
return an Array like this:

HTML::body {
[ "blahblah",
HTML::h1 {
"zomg headers"
}]
}

-or use 'return' you want to return several arguments, like

def func; return 1,2,3; end
a,b,c=func
=> a=1,b=2,c=3


Best regards,

Axel





--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/...

Ryan Lewis

5/30/2008 12:44:00 PM

0

Andreas Warberg wrote:
> Ryan Lewis wrote:
>> The code will look like this:
>> HTML::body {
>> "blahblah"
>> HTML::h1 {
>> "zomg headers"
>> }
>> }
>>
>> Assuming HTML::h1 returns "<h1>zomg headers</h1>", HTML::body will only
>> return "<h1>zomg headers</h1>".
>> But I need it to return ["blahblah", "<h1>zomg headers</h1>"]
>
> Maybe you should look into Ruby CGI. Its a part of the standard library:
> http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/...
>
> Here is a good introduction:
> http://coolnamehere.com/geekery/ruby/we...
>
> Andreas

Yeah I know, I've looked at this before i started makin this. I'm really
makin this with intentions to build something better than just an html
parser thingy.
--
Posted via http://www.ruby-....

Ryan Lewis

5/30/2008 2:58:00 PM

0

You cant use the return function within procs.

What I'm tryin to figure out is how to push each return into an array.
There /has/ to be a hack for this.
--
Posted via http://www.ruby-....

Ryan Lewis

5/30/2008 9:10:00 PM

0

David A. Black wrote:
> On Fri, 30 May 2008, David A. Black wrote:
>
>>> "bar"
>> def foo
>> yield if block_given? # no point doing it the slow way :-)
>> end
>>
>> p foo { ["bar", "baz"] }
>>
>> I have a feeling there may be something more to your question that I'm
>> not seeing.
>
> Maybe this:
>
> foo { break "bar", "baz" }
>
> ?
>
>
> David

Almost. 'break' well...breaks it though, cause im recursively calling
methods >_<
Heres my original code:
-----------------------
module HTML
def method_missing(meth, attr={}, &block)
html, attrs = "", ""

attr.keys.each{ |key|
attrs << " #{key.to_s}='#{attr[key]}'" if attr[key]
}

html << "<#{meth.to_s}#{attrs}>"
html << block.call if block #yes Dave, the long way =p for now
at least
html << "</#{meth.to_s}>"

html
end
module_function :method_missing
end

include HTML

p html {
body {
div(:class=>"divcls") { "IM IN A DIVLOL" }
}
}

=> "<html><body><div class='divcls'>IM IN A
DIVLOL</div></body></html>"

So far so good. But when I need multiple returns..
--------------------------------------------------
p html {
body {
div(:class=>"divcls") { "IM IN THE FIRST DIV" }
div(:class=>"divcls") { "IM IN THE SECOND DIV" }
}
}

=> "<html><body><div class='divcls'>IM IN THE SECOND
DIV</div></body></html>"

So now I need to break convention and use:
p html {
body {
[div(:class=>"divcls") { "IM IN THE FIRST DIV" },
div(:class=>"divcls") { "IM IN THE SECOND DIV" }]
}
}

Which is just ugly. The CGI lib overcomes this problem but I have no
idea how to make this work..
--
Posted via http://www.ruby-....

ara.t.howard

5/30/2008 9:31:00 PM

0


On May 30, 2008, at 6:22 AM, Ryan Lewis wrote:

> You dont understand, I'm making a simple HTML module for easy document
> creating.
> The code will look like this:
> HTML::body {
> "blahblah"
> HTML::h1 {
> "zomg headers"
> }
> }
>
> Assuming HTML::h1 returns "<h1>zomg headers</h1>", HTML::body will
> only
> return "<h1>zomg headers</h1>".
> But I need it to return ["blahblah", "<h1>zomg headers</h1>"]

it's already written for you:

cfp:~ > cat a.rb
require 'rubygems'
require 'tagz' ### gem install tagz @ http://codeforp...lib/ruby/tagz/tagz-4....

def HTML(*a, &b) Tagz(*a, &b) end

html =
HTML do |html|
body_{

html << "blahblah"

h1_(:color => "red"){ "zomg headers" }
}
end

puts html



cfp:~ > ruby a.rb
<body>blahblah<h1 color="red">zomg headers</h1></body>


if you really feel like reinventing the wheel read the code to see how
it's done, it's < 200 loc

http://codeforp...lib/ruby/tagz/tagz-4.2.0/l...

cheers.


a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama