Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Kelyarsky Evgeniy http://payplay.fm

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

spaces in URL

This is a workaround to get spaces in URL as pluses not %20 like it is in Rails2.

module ActionController::Routing
  class DynamicSegment
    def interpolation_chunk
      "\#{CGI.escape(#{local_name}.to_s)}"
    end

    def match_extraction(next_capture)
      default_value = default ? default.inspect : nil
      %[
        value = if (m = match[#{next_capture}])
          CGI.unescape(m)
        else
          #{default_value}
        end
        params[:#{key}] = value if value
      ]
    end
  end
end

days in month

// returns number of days in specified month

def days_in_month(month)
  (Date.new(Time.now.year,12,31).to_date<<(12-month)).day
end

remove nonprintable characters

def printable(str)
  return "" unless str
  eval(str.strip.dump.gsub(/\\[0-9]{3}/,''))
end

Ruby method to get seconds from colon-separated time string

// converts colon-separated time to seconds
// "01:02:03" => 3723

def colon_separated_to_i(str)

  time_arr = []
  time_arr = str.split(":").reverse
  sec = 0
  secs = [1, 60, 60*60, 60*60*24]

  time_arr.size.times do {|i|
    sec += secs[i].to_i * time_arr[i].to_i
  end
  sec
end
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS