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 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) |
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 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 |
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
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 |
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 'tweetstream' | |
require 'rtranslate' | |
TweetStream::Client.new('username','password').track('keyword') do |status| | |
puts Translate.t(status[:text], 'en', 'de') | |
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
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 |
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 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 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
# 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 |
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
# 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 |
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 Lucky < [String, Array][rand(2)]; end | |
puts Lucky.superclass |
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 'clojure' | |
class MyClojureObj < Clojure::Object | |
def initialize | |
dosync { @foo = 'foo' } | |
end | |
def foo; @foo; end | |
def foo=(f); @foo = f; end | |
end |