Skip to content

Instantly share code, notes, and snippets.

@jin
jin / osrc.rb
Last active August 29, 2015 14:01
require 'json'
require 'net/http'
require 'rubygems'
staffs = %w(weesun knmnyn wgx731 Leventhan franklingu Limy Muhammad-Muneer)
baseurl = "http://osrc.dfm.io/"
# Construct the json URLs
def construct_urls(base_url, usernames, suffix)
urls = Array.new
# http://www.spoj.com/problems/TEST/
guess = STDIN.gets.chomp().to_i
while guess != 42
puts guess
guess = STDIN.gets.chomp().to_i
end
@jin
jin / Xcode wifi automatic build script
Last active August 29, 2015 14:02
Automatically installing apps on your device wirelessly when they are built in Xcode with Jailbroken iPhone
#!/bin/sh
# Modified code from original at http://iphonedevwiki.net/index.php/Xcode#Automatically_installing_apps_on_your_device_wirelessly_when_they_are_built_in_Xcode
# Modify this to your device's IP address.
IP="192.168.1.67"
osascript -e 'display notification "Updating app on iPhone" with title "Xcode"'
# Verify that the build is for iOS Device and not a Simulator.
if [ "$NATIVE_ARCH" == "armv7" ]; then
@jin
jin / attribute_declaration.rb
Created June 18, 2014 09:09
better way of organizing attributes in ruby models
class User
PROPERTIES = [:id, :name, :email]
PROPERTIES.each { |prop|
attr_accessor prop
}
def initialize(attributes = {})
attributes.each { |key, value|
self.send("#{key}=", value) if PROPERTIES.member? key
}
@jin
jin / check_balanced_brackets.rb
Created October 13, 2014 16:05
Check for matching brackets in a string
def check_string(str)
str.empty? || is_balanced(0, str)
end
def is_balanced(num_left_brackets, str)
if str.head == "["
is_balanced(num_left_brackets + 1, str.tail)
elsif str.head == "]"
return false if num_left_brackets == 0
is_balanced(num_left_brackets - 1, str.tail)
@jin
jin / ruby_reduce.rb
Last active August 29, 2015 14:07
ruby Array#reduce with an initial value
class Array
def reduce(func, init)
if self.length < func.arity
res = nil
elsif self.length == func.arity
res = func.call(*self)
else
res = func.call(self[0], self[1..-1].reduce(func, nil))
end
init.nil? ? res : func.call(init, res)
#!/usr/bin/env ruby
def sum_of_differences(arr)
total = 0
arr.each_cons(2) do |x, y|
total += (x - y).abs
end
total
end
@jin
jin / functional_ruby.rb
Created November 21, 2014 18:45
Functional Ruby: implementing commonly used functions with lambda objects
# Regular Ruby methods, can't pass them around like function objects
def length(xs)
return 0 if xs.empty?
1 + length(xs[1..-1])
end
def map(xs, process = -> (x) { x })
return [] if xs.empty?
[process.call(xs.first)] + map(xs[1..-1], process)
@jin
jin / custom_comparators.rb
Last active August 29, 2015 14:10
Custom sort comparators in Ruby
def sort hash
hash.keys.map(&:to_s).sort(& -> (a, b) { a.length <=> b.length })
end
p sort ({ abc: 'hello', 'another_key' => 123, 4567 => 'third' })
@jin
jin / linked_list.rb
Created December 17, 2014 21:13
Implementing Linked List with structs
Node = Struct.new(:val, :next_node) do
def length
return 1 if next_node.nil?
1 + next_node.length
end
def to_s
self.to_a.join(", ")
end