Skip to content

Instantly share code, notes, and snippets.

@aokolish
aokolish / example.rb
Last active August 29, 2015 14:02
rails 4 argument out of range issue
# rails 3, invalid times are set to nil
> m = Model.new some_timestamp: '2010-33-22T09:30:25Z'
=> #<Model ...>
> m.some_timestamp
=> nil
# rails 4, invalid times raise an exception
> m = Model.new some_timestamp: '2010-33-22T09:30:25Z'
=> ArgumentError: argument out of range
class GeneratedPDF
def generate
# pdf stuff...
pdf_file = Tempfile.new(['something', '.pdf'])
pdf_file.binmode
pdf_file.write(pdf_string)
pdf_file
end
end
@aokolish
aokolish / _layout.html.haml
Last active August 29, 2015 14:19
Nested layout in rails
-# This file contains the 'layout' for this section of the site
%section.how-it-works-page.content-section
.container
.row
.col-md-12
%h2.section-title How It Works
.sub-nav.row
.col-sm-3
= active_link_to how_we_find_kids_path, class: 'link-wrap' do
.img.school
def replace_words(dict, sentence)
regex = /\A(#{dict.join('|')})/
sentence.split.inject([]) do |new_words, word|
if match_data = word.match(regex)
new_words << match_data.captures[0]
else
new_words << word
end
end.join(' ')
class Solution
def initialize
@known_strings = {}
end
def word_break(string, word_dict)
if @known_strings[string]
return @known_strings[string]
end
# https://leetcode.com/problems/count-primes/description/
# return the number of prime numbers less than a positive number n
def count_primes(n)
primes = []
(2..n-1).each do |i|
primes[i] = true
end
(2..Math.sqrt(n)).each do |i|
@aokolish
aokolish / closest_web_safe.rb
Created November 3, 2017 20:35
closest web safe color
WEB_SAFE_NUMBERS = [
0, # 00
51, # 33
102, # 66
153, # 99
204, # cc
255 # ff
]
def closest_web_safe_color(string)
@aokolish
aokolish / closest_web_safe.rb
Created November 3, 2017 21:14
closest web safe color...v2
WEB_SAFE_NUMBERS = [
0, # 00
51, # 33
102, # 66
153, # 99
204, # cc
255 # ff
]
def closest_web_safe_color(string)