Proc multiple returns?

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 -_-

Ryan L. 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

Andreas W. 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 “

zomg headers

”, HTML::body will only
return “

zomg headers

”.
But I need it to return [“blahblah”, “

zomg headers

”]

Ryan L. wrote:

The code will look like this:
HTML::body {
“blahblah”
HTML::h1 {
“zomg headers”
}
}

Assuming HTML::h1 returns “

zomg headers

”, HTML::body will only
return “

zomg headers

”.
But I need it to return [“blahblah”, “

zomg headers

”]

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

Here is a good introduction:
http://coolnamehere.com/geekery/ruby/web/cgi.html

Andreas

-------- Original-Nachricht --------

Datum: Fri, 30 May 2008 21:22:33 +0900
Von: Ryan L. [email protected]
An: [email protected]
Betreff: Re: Proc multiple returns?

Andreas W. wrote:

Dear Ryan,

Assuming HTML::h1 returns “

zomg headers

”, HTML::body will only
return “

zomg headers

”.
But I need it to return [“blahblah”, “

zomg headers

”]

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

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.

Andreas W. wrote:

Ryan L. wrote:

The code will look like this:
HTML::body {
“blahblah”
HTML::h1 {
“zomg headers”
}
}

Assuming HTML::h1 returns “

zomg headers

”, HTML::body will only
return “

zomg headers

”.
But I need it to return [“blahblah”, “

zomg headers

”]

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

Here is a good introduction:
http://coolnamehere.com/geekery/ruby/web/cgi.html

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.

Hi –

On Fri, 30 May 2008, Ryan L. wrote:

=> “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 :slight_smile:
end

p foo { [“bar”, “baz”] }

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

David

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

“bar”
def foo
yield if block_given? # no point doing it the slow way :slight_smile:
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

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 :slight_smile:
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” }
}
}

=> “

IM IN A
DIVLOL

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” }
}
}

=> “

IM IN THE SECOND
DIV

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…

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

Assuming HTML::h1 returns “

zomg headers

”, HTML::body will
only
return “

zomg headers

”.
But I need it to return [“blahblah”, “

zomg headers

”]

it’s already written for you:

cfp:~ > cat a.rb
require ‘rubygems’
require ‘tagz’ ### gem install tagz @
http://codeforpeople.com/lib/ruby/tagz/tagz-4.2.0/README

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

blahblah

zomg headers

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

http://codeforpeople.com/lib/ruby/tagz/tagz-4.2.0/lib/tagz.rb

cheers.

a @ http://codeforpeople.com/

Beautiful, thanks alot man!

On May 30, 2008, at 8:57 AM, Ryan L. wrote:

What I’m tryin to figure out is how to push each return into an array.

but

div_{

'this is just a value, not a return value - a noop'

[ 'this is a return value', 'and so is this' ]

}

There /has/ to be a hack for this.

nope, there doesn’t. :wink:

the best you can hope for is that this works:

div{ ‘foobar’ }

and so does this

div{ span{ ‘barfoo’ }; ‘foobar’ }

but this can never work

div{ ‘foobar’; span{‘barfoo’} }

because the ‘foobar’ is never returned or assigned it simply vanishes

a @ http://codeforpeople.com/