Rails Calendar Generation Method I have this helper method that generates an HTML calendar based on the current day:

module PageHelper
  def calendar(date)
    cal = "&lt;table class='table-condensed table-bordered table-striped'&gt;&lt;thead&gt;&lt;tr&gt;"
    days = %w(Sun Mon Tue Wed Thu Fri Sat)
    days.each do |day|
      cal += "&lt;th&gt;#{day}&lt;/th&gt;"
    end
    cal+= '&lt;/thead&gt;&lt;/tr&gt;&lt;tbody&gt;&lt;tr&gt;'
    #end of table head
    first_day = date.at_beginning_of_month.strftime('%w').to_i
    last_day = date.at_end_of_month.strftime('%d').to_i
    last_day_week = date.at_end_of_month.strftime('%w').to_i
    current_day = date.strftime("%d").to_i
    prev_month_day = (date - 1.month).at_end_of_month.strftime('%d').to_i
    # print first row
    first_day.times do |day|
      num = prev_month_day - first_day + day + 1
      cal += "&lt;td class='text-muted small'&gt;#{num}&lt;/td&gt;"
    end
    (7-first_day).times do |day|
      if day == current_day -1
        cal += "&lt;td class='btn-primary'&gt;&lt;strong&gt;#{day+1}&lt;/strong&gt;&lt;/td&gt;"
      else
        cal += "&lt;td&gt;#{day+1}&lt;/td&gt;"
      end
    end
    # print remaining rows
    cal += '&lt;tr&gt;'
    (7-first_day).upto(last_day-1).each_slice(7) do |slice|
      cal += '&lt;tr&gt;'
      slice.each do |day|
        if day == current_day -1
          cal += "&lt;td class='btn-primary'&gt;&lt;strong&gt;#{day+1}&lt;/strong&gt;&lt;/day&gt;"
        else
          cal += "&lt;td&gt;#{day+1}&lt;/td&gt;"
        end
      end
      cal += '&lt;/tr&gt;' unless slice.last == last_day -1
    end
    # get remaining number of cells
    rem_cells = 6 - last_day_week
    if rem_cells &amp;&amp; rem_cells != 6
      rem_cells.times do |day|
        cal += "&lt;td class='text-muted'&gt;#{day+1}&lt;/td&gt;"
      end
    end
    cal += '&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br/&gt;&lt;br/&gt;'
  end
end


The method is called like this:

&lt;%=  calendar(Date.today).html_safe %&gt;


Here is an example calendar:



However, I think that my code is not very DRY and the method is unnecessarily longer than what it should be.
I agree that it is too long. I find a good place to start is to try breaking it up into methods that are short and each do a single thing. While I'm doing this I usually see opportunities to make things DRY. 

My code might look like:

content_tag(:table, ...) do 
  header_row + week_rows      
end

def week_rows
  num_rows = (first_day + last_day) % 7
  rows = num_rows.times.map { |i| week_row(i) }
  content_tag(:tbody) do rows.join('')
end


A couple of things I noticed:


Instead of hard-coding html like "&lt;table class='table-condensed table-bordered table-striped'&gt;&lt;thead&gt;&lt;tr&gt;" use the Rails content_tag helper
Instead of date.at_end_of_month.strftime('%w').to_i use the wday method. There is also a day method that you can use instead of %d.
Have your method return a string that is already html_safe