Skip to content

Instantly share code, notes, and snippets.

View yangsu's full-sized avatar

Yang Su yangsu

  • San Francisco, CA
  • X @ys65
View GitHub Profile
@yangsu
yangsu / ruby-enumerable.rb
Created January 28, 2013 03:52
Ruby Enumerable
a = [5, 3, 4, 1]
a.sort # => [1, 3, 4, 5]
a.any? {|i| i > 6} # => false
a.any? {|i| i > 4} # => true
a.all? {|i| i > 4} # => false
a.all? {|i| i > 0} # => true
a.collect {|i| i * 2} # => [10, 6, 8, 2]
a.select {|i| i % 2 == 0 } # even => [4]
a.select {|i| i % 2 == 1 } # odd => [5, 3, 1]
a.max # => 5
@yangsu
yangsu / ruby-inject-example.rb
Created January 28, 2013 03:53
Ruby Inject Example
a = [5, 3, 4, 1]
a.inject(0) {|sum, i| sum + i}
# => 13
a.inject {|sum, i| sum + i}
# => 13
a.inject {|product, i| product * i}
# = 560
a.inject(0) do |sum, i|
puts "sum: #{sum} i: #{i} sum + i: #{sum + i}"
sum + i
@yangsu
yangsu / ruby-active-record.rb
Created January 28, 2013 03:53
Ruby Active Record
class Department < ActiveRecord::Base
has_many :employees
has_one :manager
end
@yangsu
yangsu / ruby-open-classes.rb
Created January 28, 2013 03:54
Ruby Open Classes
class NilClass
def blank?
true
end
end
class String
def blank?
self.size == 0
end
@yangsu
yangsu / ruby-method_missing.rb
Last active December 11, 2015 20:08
Ruby method_missing
class Roman
def self.method_missing name, *args
roman = name.to_s
roman.gsub!("IV", "IIII")
roman.gsub!("IX", "VIIII")
roman.gsub!("XL", "XXXX")
roman.gsub!("XC", "LXXXX")
(
roman.count("I") +
roman.count("V") * 5 +
@yangsu
yangsu / ruby-inheritance-macro-example.rb
Last active December 11, 2015 20:08
Ruby Inheritance/Macro Example
class Person
attr_accessor :name
def self.can_speak # This is a class method! Notice the self.
define_method 'speak' do # an instance method
puts "I can talk, my name is #{@name}!"
end
end
def initialize(name)
@name = name
@yangsu
yangsu / ruby-module-example.rb
Last active December 11, 2015 20:09
Ruby Module Example
module Person
attr_accessor :name
def self.included(base) # included is invoked whenever a module is included; base is implicit
base.extend ClassMethods # extend will add the methods defined in ClassMethods as class methods
end
module ClassMethods
def can_speak
include InstanceMethods # This includes all the instance methods
end
end
@yangsu
yangsu / ruby-tree.rb
Last active December 11, 2015 20:09
Ruby Tree Implementation
class Tree
# Define instance variables children and node_name
# and the associated getters and setters
attr_accessor :children, :node_name
# Define a constructor with 2 parameters, with the second having a default value
def initialize(name, children=[])
@children = children
@node_name = name
end
@yangsu
yangsu / javascript-number.js
Last active December 11, 2015 22:08
JavaScript Number Example
1 + 2 // 2
1e2 * 2.5 // 250
Number.MAX_VALUE / Number.MIN_VALUE // Infinity
Infinity - Infinity // NaN
-Infinity + NaN // NaN
Math.log(Math.pow(Math.E, 200)) // 200
@yangsu
yangsu / javascript-string.js
Last active December 11, 2015 22:08
JavaScript String Example
"hello".charAt(0) // h
'cat'.charAt(1) == 'cat'[1] // true
"hello, world".replace("hello", "goodbye") // goodbye, world
"hello".toUpperCase() // HELLO
'Duke PL Course'.substring(5,7) // 'PL'
"1, 2, 3".split(', ') // ['1', '2', '3']
'Duke PL Course'.indexOf('Courses') // -1