Skip to content

Instantly share code, notes, and snippets.

@panw
Last active February 21, 2017 16:07
Show Gist options
  • Save panw/d65ff9bab109a1ca6c133a5a4a6281f1 to your computer and use it in GitHub Desktop.
Save panw/d65ff9bab109a1ca6c133a5a4a6281f1 to your computer and use it in GitHub Desktop.
Ruby to JavaScript Overview
numbers = [1,2,3]
numbers.each do |num|
puts "I am #{num}"
end
double_numbers = numbers.map do |num|
num * 2
end
total = numbers.reduce(0) { |sum, num| sum + num }
key_value_pairs = {
k1: 'v1',
k2: 'v2'
}
key_value_pairs.each |key, value| do
puts value
end
class Fish
def initialize(name, species)
@name = 'Nemo'
@species = 'clown fish'
@miles_travelled = 0
end
def swim
@miles_travelled += 1
end
end
nemo = Fish.new('Nemo', 'clown fish')
# AnglerFish's Inheritance Chain
# Object -> Fish -> AnglerFish
class AnglerFish < Fish
def initialize(name, species, gender)
super
@gender = gender
@mates = []
end
def consume(male_fish)
if(@gender == 'female')
@mates.push(male_fish)
end
end
end
dory = AnglerFish.new('Dory', 'Angler', 'female')
nemo = AnglerFish.new('Nemo', 'Angler', 'male')
dory.swim
dory.consume(nemo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment