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

Todd Sayre http://del.icio.us/sporkyy

« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total

Float to HTML fraction entity

Uses HTML entities to display pretty fractional values for applicable floating point numbers.

class Float
    def html_fraction
        self.to_s.
            sub(/(.+)\.0$/,     '\1'        ).  # Zero decimal
            sub(/(-?\d+)\.25$/, '\1¼').  # One quarter
            sub(/(-?\d+)\.5$/,  '\1½').  # One half
            sub(/(-?\d+)\.75$/, '\1¾').  # Three quarters
            sub(/^(-?)0(&.+)/,  '\1\2'      )   # Strip leading zeroes
    end
end


And it works something like this:

>> (0.1).html_fraction
=> 0.1
>> (0.25).html_fraction
=> ¼
>> (0.50).html_fraction
=> ½
>> (0.75).html_fraction
=> ¾
>> (1.0).html_fraction
=> 1.0
>> (2.25).html_fraction
=> 2¼
>> (3.5).html_fraction
=> 3½
>> (4.75).html_fraction
=> 4¾
>> (5.85).html_fraction
=> 5.85
>> (-1.5).html_fraction
=> -1½
>> (-1.6).html_fraction
=> -1.6;
>> (-0.25).html_fraction
=> -¼

PrototypeHelper :with helper

This method is for use in conjuction with the Ruby on Rails Module ActionView::Helpers::PrototypeHelper.

The options hash takes any number of key/value pairs the key becomes the name of the parameter passed in the with value and the value becomes a javascript expression

key — name of the parameter
value — a fragment of javascript that will be the value of the parameter

Example
>> params_for_with(:clean_range => 'clean_range', :raw_range => 'raw_range', :total_count => 'total_count')
=> 'total_count=' + total_count + '&' + 'clean_range=' + clean_range + '&' + 'raw_range=' + raw_range


Don‘t forget you won’t necessarily get the parameters out in the same order you put them in, such is the nature of hashes

def params_for_with(options = {})
    options.collect { |param_name, js_fragment| "'#{param_name}='+#{js_fragment}" }.join("+'&'+")
    # or this one
    #options.stringify_keys.collect { |param_name, js_fragment| '"' << param_name << '="+' << js_fragment }.join('+"&"+')
end

Regular Expression for Google Groups file attachements

Links filter for DownThemAll! for matching files attached to Google Groups posts.

/\/attach\/.+part=\d+$/
« Newer Snippets
Older Snippets »
Showing 11-13 of 13 total