Skip to content

Instantly share code, notes, and snippets.

View dingsdax's full-sized avatar
🐢
slow coding

Joesi D. dingsdax

🐢
slow coding
View GitHub Profile
@dingsdax
dingsdax / random_time.rb
Created August 7, 2011 13:28
ruby random data generation
class Time
def self.random(years_back=5)
year = Time.now.year - rand(years_back) - 1
month = rand(12) + 1
day = rand(31) + 1
Time.local(year, month, day)
end
end
@dingsdax
dingsdax / gist:1118528
Created August 1, 2011 17:04
Ruby remote file checker
# from http://snippets.dzone.com/posts/show/4638
require 'rubygems'
require 'open-uri'
require 'net/http'
def remote_file_exists?(url)
url = URI.parse(url)
Net::HTTP.start(url.host, url.port) do |http|
return http.head(url.request_uri).code == "200"
@dingsdax
dingsdax / gist:1080272
Created July 13, 2011 13:13
Jaro–Winkler distance in Ruby
# extension for array class
class Array
# select array items with index
# give a block both the item with index of array
# filtered by a select statement
def select_with_index
index = -1
select { |x| index += 1; yield(x, index) }
end
# return indices array of array item
@dingsdax
dingsdax / damerau_levenshtein.rb
Created January 9, 2011 19:42
string similarity metrics in ruby
# extension for string class
class String
# return character array of string with indices
def each_char_with_index
i = 0
split(//).each do |c|
yield i, c
i += 1
end
end