could someone explain this CGI problem ?

Discussion in 'Ruby' started by lg, Jan 3, 2006.

  1. lg

    lg Guest

    the following code sequence

    cgi.div{
    1..3.times{ |i|
    cgi.div{
    "hello from div"
    }
    }
    }

    produces

    <DIV>
    1..3
    </DIV>

    but i wanted to produce

    <DIV>
    <DIV>
    huhu
    </DIV>
    <DIV>
    huhu
    </DIV>
    <DIV>
    huhu
    </DIV>
    </DIV>

    could someone point me how to get the right result ?
    lg, Jan 3, 2006
    #1
    1. Advertising

  2. lg

    Guest

    Hi --

    On Tue, 3 Jan 2006, lg wrote:

    > the following code sequence
    >
    > cgi.div{
    > 1..3.times{ |i|
    > cgi.div{
    > "hello from div"
    > }
    > }
    > }
    >
    > produces
    >
    > <DIV>
    > 1..3
    > </DIV>
    >
    > but i wanted to produce
    >
    > <DIV>
    > <DIV>
    > huhu
    > </DIV>
    > <DIV>
    > huhu
    > </DIV>
    > <DIV>
    > huhu
    > </DIV>
    > </DIV>
    >
    > could someone point me how to get the right result ?


    1..3.times is being parsed as: 1..(3.times). Since times returns its
    receiver, that's the same as: 1..3

    Try this:

    cgi.div {
    (1..3).map {
    cgi.div {
    "hello from div" # or "huhu", or whatever
    }
    }
    }

    You could also do:

    cgi.div { cgi.div { "hello from div" } * 3 }


    David

    --
    David A. Black


    "Ruby for Rails", from Manning Publications, coming April 2006!
    http://www.manning.com/books/black
    , Jan 3, 2006
    #2
    1. Advertising


  3. > the following code sequence
    >
    > cgi.div{
    > 1..3.times{ |i|
    > cgi.div{
    > "hello from div"
    > }
    > }
    > }
    >
    > produces
    >
    > <DIV>
    > 1..3
    > </DIV>


    This is, because the 1..3..... statement returns "1..3" and this is
    what cgi gets. Try it with irb.

    perhaps this works (untested)



    > cgi.div{

    temp_string=""
    > 1..3.times{ |i|

    temp_string << cgi.div{ "hello from div" }
    > }

    temp_string
    > }


    i.e. it collects the divs in a temporary string and gives that to the
    outer div.

    Patrick
    Patrick Gundlach, Jan 3, 2006
    #3
  4. lg

    John Maclean Guest

    Understanding the fundamentals, methods. (from Pickaxe book)

    Hey chaps,

    I'd just like to check that the following uses methods and that no classes or instance variables are created. I see that there are no @instance_variables..
    Where's the object? What class is being used?
    Am I correct in saying that say_goodnight is a method, where (name) is the parameter for it?

    #!/usr/bin/ruby
    # Tue Dec 27 15:42:59 GMT 2005
    # from page 13 of the pick-axe book
    def say_goodnight(name)
    "Good night, #{name.capitalize}"
    # we use the output of the last result - this save time
    end
    # Time for bed...
    puts say_goodnight('jayeola')
    puts say_goodnight('john-Boy')
    puts say_goodnight('mary-Ellen')
    puts say_goodnight('mary-loo')
    puts say_goodnight('silly-slapper')
    puts say_goodnight('hex-editor')


    --
    John Maclean
    MSc (DIC)
    07739 171 531
    John Maclean, Jan 3, 2006
    #4
  5. lg

    lg Guest

    thanks - this was the hint i needed !

    regards,

    lars
    lg, Jan 3, 2006
    #5
  6. lg

    Kero Guest

    Re: Understanding the fundamentals, methods. (from Pickaxe book)

    > I'd just like to check that the following uses methods and that no classes
    > or instance variables are created. I see that there are no
    > @instance_variables..
    >
    > Where's the object?


    try
    ruby -e 'p self'
    (as if executing a ruby file containing "p self" and nothing else)
    it prints "main".
    Apparently the object sees itself as main. ok.

    > What class is being used?


    do
    ruby -e 'p self.class'
    it prints "Object"
    so main is an Object. tadaa!
    (actually, it is a bit more than "just" an Object, but you can figure that
    out yourself when you learn about Modules and mixin)

    What should you learn from this? Everything is an object. Really. But if you
    want to do some simple procedural stuff, the objects do not get in your way.
    They can be, as you experienced, completely invisible. That's one of many,
    many reasons I like Ruby so much :)


    > Am I correct in saying that say_goodnight is a method, where (name) is the
    > parameter for it?


    absolutely correct

    > #!/usr/bin/ruby
    > # Tue Dec 27 15:42:59 GMT 2005
    > # from page 13 of the pick-axe book
    > def say_goodnight(name)
    > "Good night, #{name.capitalize}"
    > # we use the output of the last result - this save time
    > end

    [snip]
    Kero, Jan 4, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gactimus
    Replies:
    22
    Views:
    712
    jeffc
    Dec 15, 2004
  2. Sean Berry
    Replies:
    3
    Views:
    284
    Tor Iver Wilhelmsen
    Apr 28, 2004
  3. Replies:
    5
    Views:
    278
    Bo Persson
    Jan 10, 2006
  4. Alexander Mahone

    Could someone please explain me this error?

    Alexander Mahone, May 2, 2008, in forum: C Programming
    Replies:
    5
    Views:
    264
    Igmar Palsenberg
    May 5, 2008
  5. wkevin
    Replies:
    4
    Views:
    570
    Edward A. Falk
    Nov 7, 2011
Loading...

Share This Page