Proc multiple returns?

R

Ryan Lewis

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

David A. Black

Hi --

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
 
D

David A. Black

Hi --

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
 
A

Andreas Warberg

Ryan said:
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
 
R

Ryan Lewis

Andreas said:

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>"]
 
A

Andreas Warberg

Ryan said:
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/index.html

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

Andreas
 
A

Axel Etzold

-------- Original-Nachricht --------
Datum: Fri, 30 May 2008 21:22:33 +0900
Von: Ryan Lewis <[email protected]>
An: (e-mail address removed)
Betreff: Re: Proc multiple returns?

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
 
R

Ryan Lewis

Andreas said:
Ryan said:
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/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.
 
R

Ryan Lewis

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.
 
R

Ryan Lewis

David said:
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..
 
A

ara.t.howard

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://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
<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://codeforpeople.com/lib/ruby/tagz/tagz-4.2.0/lib/tagz.rb

cheers.


a @ http://codeforpeople.com/
 
A

ara.t.howard

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. ;-)


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/
 

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

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top