Skip to content

Instantly share code, notes, and snippets.

View makaroni4's full-sized avatar

Anatoli Makarevich makaroni4

View GitHub Profile
@makaroni4
makaroni4 / bindex.rb
Created June 21, 2012 16:45
Source of article about binary search at Gistflow.com
class Array
def bindex element, lower = 0, upper = length - 1
return if lower > upper
mid = (lower + upper) / 2
element < self[mid] ? upper = mid - 1 : lower = mid + 1
element == self[mid] ? mid : bindex(element, lower, upper)
end
end
class Array
@makaroni4
makaroni4 / methods_wrapper.rb
Created April 26, 2012 09:11
Wrap instance methods of object to log them
class User
def say
puts "Hello"
end
end
def override_instance_methods(c)
c.instance_methods(false).each do |m|
c.class_eval <<-RUBY
alias #{m}_original #{m}
@makaroni4
makaroni4 / caesar_cypher.rb
Created April 21, 2012 18:22
Caesar cypher for Ruby
class String
def to_caesar shift
shift = (shift % 26).abs
each_byte.map { |b| ((b + shift - 97) % 26 + 97).chr }.join
end
end
a = "Caesar was a cool guy"
p a.split.map { |w| w.to_caesar 4 }.join(' ') # => "aeiwev aew e gssp kyc"
@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
@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 / 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 / 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 / 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 / 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 / Gemfile
Created February 21, 2012 08:31
Dictionary sinanra app based on Bing API
source 'http://rubygems.org'
gem 'sinatra'
gem 'haml'