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

···

--
Posted via http://www.ruby-forum.com/.

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 :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, Ryan Lewis wrote:

--
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.rubypal.com for details and updates!

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-forum.com/\.

Maybe this:

   foo { break "bar", "baz" }

?

David

···

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

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

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

--
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.rubypal.com for details and updates!

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-forum.com/.

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

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

Andreas

···

--
Posted via http://www.ruby-forum.com/\.

-------- 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: GMX Browser - verwenden Sie immer einen aktuellen Browser. Kostenloser Download.

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

  => "<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-forum.com/\.

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/

···

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

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

···

--
Posted via http://www.ruby-forum.com/\.

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-forum.com/.

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/

···

On May 30, 2008, at 8:57 AM, Ryan Lewis wrote:
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Beautiful, thanks alot man!

···

--
Posted via http://www.ruby-forum.com/.