Skip to content

Instantly share code, notes, and snippets.

@softr8
softr8 / gist:972462
Created May 14, 2011 18:07
ruby 1.9.2 and ticketmaster
ruby-1.9.2-p180 :009 > driver.projects.first.tickets
RuntimeError: can't typecast "252"
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:96:in `typecast_xml_value'
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:118:in `block in typecast_xml_value'
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:117:in `each'
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:117:in `inject'
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:117:in `typecast_xml_value'
from /Users/ecruz/.rvm/gems/ruby-1.9.2-p180@panorama/gems/activesupport-3.0.6/lib/active_support/core_ext/hash/conversions.rb:78:in `from_xml'
from /Users/ecruz/.rvm/gems/ruby-1
@softr8
softr8 / gist:1035981
Created June 20, 2011 16:53
Mocking methods at instance level
class Una
def get
"Esta es una prueba"
end
end
dos = Class.new(Una) do
def get
"desde aca"
end
@softr8
softr8 / gist:2955680
Created June 19, 2012 18:13
Rails cache wrapper
module MyApp
# See ActiveSupport::Cache::Store for documentation.
module Cache
class << self
def fetch(key, options = {}, &block)
if block_given?
if ActionController::Base.perform_caching && options && options[:expires_in].to_i > 0
Rails.cache.fetch(key, options, &block)
else
yield
@softr8
softr8 / gist:3815777
Created October 2, 2012 02:12
SG 1 oct
# Como usarlo:
# ruby sleep.rb "6:15 AM"
# ruby sleep.rb
require 'date'
require 'time'
desired_hour = ARGV.first || "7 AM"
wakeup_at = Time.parse("#{Date.today} #{desired_hour}")
@softr8
softr8 / gist:6466376
Created September 6, 2013 16:39
nginx config params
./configure \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--user=www-data \
--group=www-data \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module \
--without-http_fastcgi_module \
@softr8
softr8 / gist:6539707
Last active December 22, 2015 22:19
Roman to integers in ruby
def solution(roman)
roman.scan(/IV|IX|CM|XC|I|V|L|X|C|M|D/).inject(0) do |final, current|
final += case current
when 'IV' then 4
when 'IX' then 9
when 'XC' then 90
when 'CM' then 900
when 'I' then 1
when 'V' then 5
when 'X' then 10
@softr8
softr8 / gist:6622944
Created September 19, 2013 12:48
pascal's triangle in ruby
def pascalsTriangle(n)
(1..n).inject([]) do |final, level|
final << (1..level).inject([]) do |horizontal, index|
if index == 1 || index == level
horizontal << 1
else
horizontal << (final[level - 2][index - 2]) + (final[level - 2][index - 1])
end
end
final
@softr8
softr8 / gist:6625828
Created September 19, 2013 16:16
hash ._magic accessor
class Hash
def method_missing( symbol, *args )
a, method = symbol.to_s.split('_')
if method
if self.has_key?(method.to_sym) || self.has_key?(method)
self[method.to_sym] || self[method]
else
if method.include?('=')
key = method.split('=').first.strip
if has_key?(key)
@softr8
softr8 / gist:7042584
Last active December 25, 2015 21:29
faster kaminary pagination view helper replacement
def faster_paginate collection
output = ''
if collection.num_pages > 1
output << "<nav class='pagination'>"
page_base = collection.current_page
if page_base > 4
output << content_tag(:span, class: 'first') do
link_to raw(t 'views.pagination.first'), url_for(params.merge(page: 1))
end
output << content_tag(:span, class: 'page gap') { '... '}
@softr8
softr8 / gist:7043527
Created October 18, 2013 15:49
Measure gem loading time with bundler, add this at the top of your application.rb
if ENV['DEBUG_GEMLOADING']
Bundler::Runtime.class_eval do
def Kernel.require file
puts Benchmark.measure("#{file}") {
super
}.format("%t - %n: %r")
end
end
end