This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
include Test::Unit::Assertions | |
class Array | |
def transpose | |
return [] if empty? | |
a = Array.new(self.first.size) {[]} | |
each do |e| | |
begin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("input[type='checkbox']").each(function(){ | |
$(this).attr('checked', true); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Integer | |
def odd? | |
% 2 == 0 | |
end | |
end | |
def middle_index(input) | |
size = input.size | |
if size.odd? | |
size/2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
def my_own_inject(proc) | |
temp = 0 | |
self.each do |e| | |
temp = proc.call(temp, e) | |
end | |
temp | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gem 'sinatra' | |
gem 'haml' |