Skip to content

Instantly share code, notes, and snippets.

View fdelacruz's full-sized avatar

Francisco De La Cruz fdelacruz

View GitHub Profile
xcode-select --install
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew update
brew cask install iterm2
# update iterm2 settings -> solarized presets
brew install git
brew cask install spectacle
brew cask install alfred
# set CMD+space to launch alfred
brew cask install firefox
def proc_test
proc = Proc.new { return }
proc.call
puts "Hello world"
end
proc_test # calling proc_test prints nothing
def lambda_test
lam = lambda { return }
lam.call
puts "Hello world"
end
lambda_test # calling lambda_test prints 'Hello World'
proc = Proc.new { |x| puts x } # creates a proc that takes 1 argument
proc.call(2) # prints out 2
proc.call # returns nil
proc.call(1,2,3) # prints out 1 and forgets about the extra arguments
lam = lambda { |x| puts x } # creates a lambda that takes 1 argument
lam.call(2) # prints out 2
lam.call # ArgumentError: wrong number of arguments (0 for 1)
lam.call(1,2,3) # ArgumentError: wrong number of arguments (3 for 1)
lambda { puts "Hello!" }
#Is just about the same as
Proc.new { puts "Hello!" }
cube = Proc.new { |x| x ** 3 }
[1, 2, 3].collect!(&cube) #=>[ 1, 8, 27]
[4, 5, 6].map!(&cube) #=>[64, 125, 216]
array = [1, 2, 3, 4]
array.collect! do |n|
n ** 2
end
puts array.inspect # => [1, 4, 9, 16]
class Gear
attr_reader :chainring, :cog, :rim, :tire
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@rim = rim
@tire = tire
end
def ratio
arr = [1, 2, 3, 4]
new_arr = arr.map { |x| x * 3 }
puts new_arr.inspect # => [3, 6, 9, 12]