Skip to content

Instantly share code, notes, and snippets.

@eljojo
eljojo / hash_manipulation.rb
Created December 8, 2015 19:41
some playing with functional programming and ruby
# A HashManipulation is a helper object.
# The idea is to aid when modifying hashes.
# changes = HashManipulation.new
# changes.key { |key| key.upcase }
# changes.value { |value| value.length }
# changes.("children" => [1, 2, 3]) # {"CHILDREN" => 3}
module Reports
class HashManipulation
def initialize
@key = -> (obj) { obj }
@eljojo
eljojo / application_helper.rb
Created March 26, 2013 18:50
random emoji application helper
module ApplicationHelper
# hand picked from http://www.grumdrig.com/emoji-list/
RANDOM_EMOJI = %w{🚳 🏊 🍕 🍔 🍟 🌽 🍉 🎣 🎅 👻 🎃 🐎 🐴 🐒 🐵 🐗 🐷 🐻 👴 🙀 👳 👮 👲 💏}
def random_emoji(count = 5)
RANDOM_EMOJI.shuffle[0..count].join(' ')
end
end
@eljojo
eljojo / replacing.rb
Created April 9, 2013 17:53
Replacing backreferences in Regular Expressions with its matched content.
require 'pp'
text = 'and the day is 08/11/1993 of course'
regex = 'the (day) (.+) <date> of'
result = regex.dup
until (s ||= 0) >= result.length
break unless section = (/(\((?!\?)(?:[^\(]+?[^\\]\)))/).match(result, s)
matches = Regexp.new(result.gsub(/<.+?>/, '.+?')).match(text)
offset = section.offset(0)
@eljojo
eljojo / humanize_seconds.rb
Created June 24, 2013 14:15
How to generate a human readable time range using ruby on rails
# modification of http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails
def humanize_seconds secs, precision = 2
return unless secs
units = [[60, :second], [60, :minute], [24, :hour], [1000, :day]]
diffs = units.map do |count, name|
next if units.find_index([count, name]) > precision
if secs > 0
secs, n = secs.divmod(count)
pluralize(n.to_i, name.to_s) if n > 0
end
@eljojo
eljojo / http_client.rb
Created July 5, 2013 08:40
simple ruby http client that allows you to use :TLSv1, for old servers that fail with OpenSSL 1.0.1e. https://raw.github.com/eljojo/polar_express/master/lib/polar_express/http_client.rb
class HTTPClient
attr_accessor :old_tls, :use_ssl
def initialize(opts = {})
config = {
old_tls: true,
use_ssl: nil,
}.merge(opts)
@old_tls = config[:old_tls]
@eljojo
eljojo / amazon_item.rb
Last active December 19, 2015 15:29
Simple Amazon Product Advertisement api client, using Vacuum and Nokogiri. https://github.com/hakanensari/vacuum
class AmazonItem
attr_accessor :asin, :url, :manufacturer, :category, :title, \
:ean, :item_number, :model, :lowest_price, :image_url, \
:list_price, :offer_price
def initialize(attributes = {})
update_attributes(attributes)
end
def update_attributes(new_attributes)
@eljojo
eljojo / frown.coffee
Created August 21, 2013 10:48
show frown face
show_frown_gif = ->
@result.html $('<img>').attr('src', 'http://mrwgifs.com/wp-content/uploads/2013/07/Zooey-Deschanels-Cute-Sad-Frown.gif')
@eljojo
eljojo / string_each_match.rb
Created September 1, 2013 17:12
String#each_match, kind of like String#scan but returning MatchData instead of the simple match. It takes a block as well!
class String
def each_match(regex)
result = []
position = 0
while match_data = regex.match(self, position) do
result << if block_given? then yield(match_data) else match_data end
position = match_data.end(match_data.size - 1)
end
@eljojo
eljojo / 0_reuse_code.js
Created July 5, 2017 13:17
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console