Skip to content

Instantly share code, notes, and snippets.

🏠
Working from home

Dan Dimerman dimerman

🏠
Working from home
Block or report user

Report or block dimerman

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Hash.from_xml using Nokogiri
# USAGE: Hash.from_xml:(YOUR_XML_STRING)
require 'nokogiri'
# modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
class Hash
class << self
def from_xml(xml_io)
begin
result = Nokogiri::XML(xml_io)
return { result.root.name.to_sym => xml_node_to_hash(result.root)}
@dimerman
dimerman / gist:2052720
Created Mar 16, 2012
Resizing an image to a given rectangle maintaining aspect ratio
View gist:2052720
def fit_dimensions( original, target )
original_ratio = original[:height].to_f / original[:width].to_f
target_ratio = target[:height].to_f / target[:width].to_f
result = target.clone
case original_ratio <=> target_ratio
when -1 then result[:height] = target[:width].to_f * original_ratio
when 1 then result[:width] = target[:height].to_f / original_ratio
end
return result
end
You can’t perform that action at this time.