Posted on December 07, 2007

Drop the following code into your application helper and application.js for Less/More links in the style of YouTube descriptions (see to the right of any video).
In your application_helper.rb:
module ApplicationHelper
# YouTube-style (less) ... (more) links for descriptions, etc
def less_more(text, max=75)
return text if text.size < max
trimmed = truncate(text, max)
id = rand(500000) # 1 in 500,000 chance of collision (if using more than once per page) =)
html = <<HTML
<span id="#{id}-preview">#{trimmed} #{more_link(id)} </span>
<span id="#{id}-full" style="display: none;">#{text} #{less_link(id)} </span>
HTML
return html
end
def more_link(id)
"(<a href=\"javascript:more('#{id}');\">more</a>)"
end
def less_link(id)
"(<a href=\"javascript:less('#{id}');\">less</a>)"
end
end
In your application.js:
function less(id) {
Element.hide(id + '-full');
Element.show(id + '-preview');
}
function more(id) {
Element.hide(id + '-preview');
Element.show(id + '-full');
}

Tagged with: lolcats |
Posted on October 10, 2007

One of the things I love about Ruby is the ability to easily daisy-chain several functions together. This feature is by no means unique to Ruby, but here I’ll show how one might do this the Ruby way.
You have an array representing days of the week, indexed by 0 – 7, that you get back from a web post as strings.
days = params[:days] # {"5" => "1", "6" => "1", "2" => "1"}
But we want the keys as an array of integers in sorted order.
One way would be:
keys = days.keys
sorted = keys.sort
ints = []
sorted.each { |i| ints << i.to_i }
Of course you wouldn’t actually ever do it this way once you’re familiar with Ruby idioms and are comfortable reading daisy-chained code (it can get ugly if abused!).
In Ruby, the above four lines can be condensed into the following one-liner:
ints = days.keys.sort.map(&:to_i)
Note: Rails monkeypatches collect/map to allow for the map(&:to_i) shortcut.
Tagged with: lolcats |
Posted on September 05, 2007

These are HTTP ETags—via Wikipedia:
An ETag (entity tag) is an HTTP response header returned by an HTTP/1.1 compliant web server to determine if the URL cached is identical to the requested URL on the server. The header is useful for intermediary devices that perform caching, as well as client web browsers that cache results. One method of generating the ETag is based on the last modified time of the file and the size of the file.
I’ve wondered what these were before but didn’t know exactly until a friend sent me the above link.
Tagged with: lolcats |
Posted on August 31, 2007

Let’s say you’re working on a small team and need to divvy up your models and controllers to do a code sweep.
You want to place these in a Google Docs spreadsheet or something. The code:
files = []
Dir["app/controllers/*"].each do |f|
f = f.gsub('app/controllers/', '')
files << f
end
Dir["app/models/*"].each do |f|
f = f.gsub('app/models/', '')
files << f
end
files.each do |f|
puts f
end
Note: does not handle nested subdirectories within your app/models/ or app/controllers/ directories.
Tagged with: lolcats |
Posted on August 13, 2007

Have an array of strings that you’d easily like to turn into an array of syms?
%w[total total_found time].map(&:to_sym)
# Result: [:total, :total_found, :time]
Have an array of arrays out of which you’d like to easily extract the first item from?
[[1, 'One'], [2, 'Two'], [3, 'Three']].map(&:first)
# Result: [1, 2, 3]
These will also work with the collect and map! functions.
More here on the map function and its origins in functional programming languages like lisp.
Tagged with: lolcats |