SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    can i return multiple values from helper method?

    So I'm trying to clean up my views, removing logic and such.

    Here's what I currently have
    Code Ruby:
    <% if counter == 7 -%><% counter = 0 -%></tr><tr><% end -%>

    And here's my attempt at a helper
    Code Ruby:
      def week_row_end(counter)
        if counter == 7
         counter = 0 
         row_end = "</tr><tr>"
        else
         counter += 1 
        end 
      end

    which works…*but only once. The counter being set back to 0 isn't getting sent because the row_end is instead (since it's the last one, correct?). SO, in this instance, is it just necessary to have logic in the view sometimes?

    By the way, the goal here is to end a row for a calendar and start a new one.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,448
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)
    My guess is that it has more to do with scope. That is, is "counter" being passed by reference, value, pointer ???? (I tried looking online and only got more confused instead of an answer).

    I saw something about ints being immutable, so maybe
    counter += 10
    won't work

    I wonder if something like
    counter.value =
    might do the trick

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think all you need to do is make counter an instance variable by changing it to:
    Code:
    @counter
    I think something like this would work:
    Code:
    def week_row_end(counter)
        @counter = 0 unless @counter
        if @counter == 7
         @counter = 0
         row_end = "</tr><tr>"
        else
         @counter += 1
         row_end = ""
        end
        return row_end
    end

  4. #4
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or you could try something like:

    counter = (counter + 1) &#37; 7

    always yielding a number between 0 and 6 in counter, then decide whether 0 or six triggers your rowend.

    Or, if you're feeling expansive, you could generate an array for the Calendar days (let's assume you want Sunday as 1st day of week and the month starts on Tues, the array starts with 0,0,1,.... to indicate the blank days, likewise zero- fill the back end till it reaches a multiple of 7). Then you could slice the top 7 elements of the array and feed them to a partial which starts the row, and feeds each day to a partial that renders the day, and after the second partial finishes the first closes the row.

    Strictly speaking, the function you gave is faulty, because the the week_row doesn't end with "</tr><tr>", it ends with "</tr>" and the next row shouldn't be started unless you have data for it.

  5. #5
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strictly speaking, the function you gave is faulty, because the the week_row doesn't end with "</tr><tr>", it ends with "</tr>" and the next row shouldn't be started unless you have data for it.
    Right now the if statement is at the beginning of the block, so a new row isn't getting created if there isn't data.

    Thanks for both suggestions.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •