Skip to content

Instantly share code, notes, and snippets.

View makaroni4's full-sized avatar

Anatoli Makarevich makaroni4

View GitHub Profile
@makaroni4
makaroni4 / flash_session_cookie_middleware.rb
Created July 22, 2011 13:13 — forked from Bertg/flash_session_cookie_middleware.rb
FlashSessionCookieMiddleware with fix for sessions with url encoded tokens
@makaroni4
makaroni4 / lastfm_events.rb
Created February 12, 2012 19:40
Get all the events in location from last.fom
#ruby
require "rubygems"
require "net/http"
require 'nokogiri'
#API
PARAMS = %w(title website description)
API_KEY = "YOUR KEY"
LOCATION = "moscow"
@makaroni4
makaroni4 / set_variable_callback.rb
Created February 20, 2012 17:21
Add method when set variable using monkey patching
class Module
def fields_callbacks(fields)
fields.each do |field, value|
class_eval do
alias :"old_#{field}=" :"#{field}="
define_method("#{field}=") do |v|
value.each { |method| send(method) }
send("old_#{field}=", v)
end
end
@makaroni4
makaroni4 / Gemfile
Created February 21, 2012 08:31
Dictionary sinanra app based on Bing API
source 'http://rubygems.org'
gem 'sinatra'
gem 'haml'
@makaroni4
makaroni4 / proc_inject.rb
Created March 10, 2012 10:21
Inject using proc
class Array
def my_own_inject(proc)
temp = 0
self.each do |e|
temp = proc.call(temp, e)
end
temp
end
end
@makaroni4
makaroni4 / qsort_comp.rb
Created April 2, 2012 07:43
Comparison count in Qsort algorithm
class Integer
def odd?
% 2 == 0
end
end
def middle_index(input)
size = input.size
if size.odd?
size/2
@makaroni4
makaroni4 / check_checker.js
Created April 2, 2012 09:59
Check all checkboxes on page
$("input[type='checkbox']").each(function(){
$(this).attr('checked', true);
});
@makaroni4
makaroni4 / pack.rb
Created April 4, 2012 20:33
How to find out pack argument
# assert_equal "あいうえお".unpack('__'), [12354, 12356, 12358, 12360, 12362]
['S', 'L', 'Q', 'c', 's', 'l', 'q', 'S_', 'S!', 'I', 'I_', 'I', 'L_', 'L!', 's_', 's!', 'i', 'i_', 'i', 'l_', 'l!', 'S>', 'L>', 'Q>', 's>', 'l>', 'q>', 'S!>', 'I!>', 'L!>', 's!>', 'i!>', 'l!>', 'S<', 'L<', 'Q<', 's<', 'l<', 'q<', 'S!<', 'I!<', 'L!<', 's!<', 'i!<', 'l!<', 'n', 'N', 'v', 'V', 'U', 'w', 'a', 'A', 'Z', 'B', 'b', 'H', 'h', 'u', 'M', 'm', 'P', 'p', '@', 'x', 'X', 'D', 'd', 'F', 'f', 'E', 'e', 'G', 'g'].each do |a|
begin
p "あいうえお".unpack("#{a}*")
rescue
end
end
@makaroni4
makaroni4 / array_transpose.rb
Created April 6, 2012 11:04
Implementation of Array#transpose
require 'test/unit'
include Test::Unit::Assertions
class Array
def transpose
return [] if empty?
a = Array.new(self.first.size) {[]}
each do |e|
begin
@makaroni4
makaroni4 / copy_object.rb
Created April 13, 2012 19:36
Different ways to copy object in Ruby
h = {}
h[:label] = "Red"
h_copy = h
h_dup = h.dup
h_clone = h.clone
h_deep_copy = Marshal.load( Marshal.dump h )
p h[:label].object_id # => 2156268500
p h_dup[:label].object_id # => 2156268500