This file contains 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 NestedHashEnumerator | |
include Enumerable | |
def initialize(obj) | |
@obj=obj | |
end | |
def each(&block) | |
@obj.each { |k,v| Hash === v ? NestedHashEnumerator.new(v).each(&block) : yield k,v } | |
end |
This file contains 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
key == 'size' / | |
? item.type == 'image' ? 'image item' : | |
image.type == 'location' ? 'map' : 'item thumbnail' | |
: key == 'patch' ? "items/#{item.id}" : nil |
This file contains 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
#!/usr/bin/env ruby | |
require "librevox" | |
require "sequel" | |
DB = Sequel.connect('postgres://postgres:leenuhks@localhost/freeswitch') | |
class Outbound < Librevox::Listener::Outbound | |
def session_initiated | |
answer |
This file contains 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 'socket' | |
require 'uri' | |
class HttpClient | |
include Socket::Constants | |
def initialize(uri) | |
@parsedURI = parseURI(uri) | |
@socket = Socket.new(AF_INET, SOCK_STREAM, 0) | |
socketAddress = Socket.pack_sockaddr_in(80,@parsedURI[2]) |
This file contains 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 Graph | |
# root is the vertex we start searching on, problem takes a vertex as an | |
# argument and returns a true value when it is solved | |
# the search returns either false or the solution of the problem. | |
def self.breadth_first_search(root, problem) | |
fringe = [root] | |
visited = [] | |
until fringe.empty? | |
solution = fringe.detect {|vertex| problem[vertex] } | |
return solution if solution |
This file contains 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
suite "Testing floats" do | |
exercise { 0.18 - 0.01 } | |
verify != 0.17 | |
verify.within_delta 0.001 | |
end |
This file contains 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
def substr(str1, str2) | |
len = str1.length | |
last = (0...len).detect{|i| str1[i] != str2[i]} | |
last ||= len | |
str1[0...last] | |
end | |
def gcp(strings) | |
sorted = strings.sort | |
substr(sorted.first, sorted.last) |
This file contains 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
#!/usr/local/bin/ruby | |
print ARGV.join(" ") + " #=> " | |
begin | |
p(eval(ARGV.join(" "),binding,"(demo)")) | |
rescue Exception => e | |
puts "#<#{e.class}: #{e.message[/.*/]}>" | |
end |
This file contains 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 File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb') | |
describe Account do | |
let(:user) { Factory.create(:account) } | |
describe Account, "create" do | |
describe "with username and password" do | |
subject { Factory.build(:account, :email => nil) } | |
it { should be_valid } | |
end |
This file contains 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
def short_circuit_fetch(*keys) | |
result = self | |
keys.each do |key| | |
result = result[key] | |
break unless result | |
end | |
result | |
end |