i've noticed..

2011, Feb 28

Convert Time to a Fraction

If you come across the need to store time as a float in ruby (I won’t judge you), you can use this bit of code. It assumes that each subpart of the time string is 60 parts of the previous part. This works for hours, minutes and seconds, regardless of how it is entered, and will always use the first part as the base measurement.

j = 0

timer = 0

"12:40:30".split(':').each do |y|

timer += y.to_f / (60**j); j += 1;

end

In this example, the numbers 12, 0.66, and 0.008 are summed to give 12.6724 hours.

2010, Jun 11

Check to see if a domain name is available (ruby)

# Check to see if a domain is available (unregistered)
def available?(domain_name)
  # Use Net::DNS library via ruby gems
  require 'rubygems'
  require 'net/dns/resolver'
  res = Net::DNS::Resolver.new
  # Use Google public DNS for speed
  res.nameservers = ["8.8.8.8","8.8.4.4"]
  res.udp_timeout=(60)
  packet = res.search(domain_name, Net::DNS::NS)
  # Check for domains packets with Answers
  # This means a domain returned a DNS record (registered)
  if(packet.header.anCount > 0)
    return false
  else
    return true
  end
end

2010, Feb 23

Highlight Text As You Type [JavaScript]

I wrote this technique to highlight text on a webpage as you type. It is a little clunky, but may get you off on the right foot. Sorry it isn’t tabbed properly. Tumblr is not the best for posting code.

Basically: You have a form that, onKeyUp(), runs a function to change the style of text that matches text in the form field. The one problem is that it doesn’t ignore html, so it may highlight the “br” from “<br/>” or other tags.

Read More