Skip to content

Instantly share code, notes, and snippets.

View citizen428's full-sized avatar

Michael Kohl citizen428

View GitHub Profile
@citizen428
citizen428 / LazyLists.rb
Created June 18, 2009 09:34
Quick and dirty lazy lists in Ruby, based on Hashes. This is just a quick proof of concept hack, not a real implementation.
class LazyList
def initialize(&block)
@ll = Hash.new &block
end
def take(x)
(1..x).inject([]) { |ret, i| ret << @ll[i] }
end
def take_from(x,y=1)
def get_random_char
(r = rand(36)) < 26 ? (?a+r).chr : (?0+r-26).chr
end
# building a string using the above method
def generate_string(len)
raise ArgumentError if len < 1
(1..len).map { get_random_char }.join
end
Number dividesBy = (x):
self % x == 0.
sum = 0
1 to 999 (x):
if (x dividesBy(3) || x dividesBy(5)): sum += x
_x
(sum, "\n") join print
require 'tweetstream'
require 'rtranslate'
TweetStream::Client.new('username','password').track('keyword') do |status|
puts Translate.t(status[:text], 'en', 'de')
end
db = Connection.new(ENV['DATABASE_URL']).db(db_name)
if ENV['DATABASE_USER'] && ENV['DATABASE_PASSWORD']
auth = db.authenticate(ENV['DATABASE_USER'], ENV['DATABASE_PASSWORD'])
end
class Polynomial
def initialize(coefficients)
raise ArgumentError, "Need at least 2 coefficients" if coefficients.size < 2
@co = coefficients
@powers = Array.new(@co.size - 2) { |i| "x^#{i+2}"}.reverse << 'x' << nil
end
def to_s
return "0" if @co.all? { |c| c.zero? } # not much to do in this case
@co.zip(@powers).map do |el|
# This is how you can get a user's location using MacRuby and CoreLocation
framework 'CoreLocation'
def locationManager(manager, didUpdateToLocation: new_location, fromLocation: old_location)
puts "location: #{new_location.description}"
exit
end
loc = CLLocationManager.alloc.init
loc.delegate = self
# Reincarnation for classes
class Class
def reincarnate
buried = Object.__send__(:remove_const, self.name)
Object.const_set(self.name, Class.new(buried))
end
end
class Abc
class Lucky < [String, Array][rand(2)]; end
puts Lucky.superclass
require 'clojure'
class MyClojureObj < Clojure::Object
def initialize
dosync { @foo = 'foo' }
end
def foo; @foo; end
def foo=(f); @foo = f; end
end