Skip to content

Instantly share code, notes, and snippets.

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
key == 'size' /
? item.type == 'image' ? 'image item' :
image.type == 'location' ? 'map' : 'item thumbnail'
: key == 'patch' ? "items/#{item.id}" : nil
#!/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
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])
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
suite "Testing floats" do
exercise { 0.18 - 0.01 }
verify != 0.17
verify.within_delta 0.001
end
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)
#!/usr/local/bin/ruby
print ARGV.join(" ") + " #=> "
begin
p(eval(ARGV.join(" "),binding,"(demo)"))
rescue Exception => e
puts "#<#{e.class}: #{e.message[/.*/]}>"
end
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
def short_circuit_fetch(*keys)
result = self
keys.each do |key|
result = result[key]
break unless result
end
result
end