Upload
brian-douglas
View
67
Download
1
Tags:
Embed Size (px)
Citation preview
Odin Ruby Study Group
Hello I am Brian
I will be you guide@brianllamar
ilikerobot[at]gmail
Extras
Sign up for Odin G+ CommunitySign up for Ruby Newbie Group- both will assist in helpingWhile you are at it Sign up for the Ruby Community.
Odin Project is open sourced
In Ruby Everything is an Object
Define Car Class
class Car# code goes inside
end
create Car parts
class Cardef initialize # method
# part go hereend
end
Create car parts
class Cardef initialize # method
@engine = Engine.new @wheels = 4.times.map { Wheel.new }end
end
Let get Proceduaral (Law & Order)
My approach is always different# # If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5,
6 and 9. The sum of these multiples is 23.
# # Find the sum of all the multiples of 3 or 5 below 1000.
# Pseudo
# set range 1..1000
# set the if statment to find the multiples of 3 and 5
# use if statement to add nums divisible by 3 and 5 to be added to a new_range array.
# create method that adds the new_range numbers to create a sum.
# call the Multiple class using the muli_pulls method and sum_of_multiples method
class Multiples
def multi_pulls
# set range variable 1..1000
numbers = Array(1...1000) # inclusive range
multi_pulls = [] # create a new array
for i in numbers # loop that runs through every number in the array
if i % 3 == 0 || i % 5 == 0 # finds only numbers in array that are multiples of 3 and 5
multi_pulls << i # add those multiples to the new array
end
end
multi_pulls # puts final multiple array
end
def sum_of_multiples(multi_pulls) # method to find the sum of the multipes array
sum = 0 # create sum variable
multi_pulls.each {|i| sum += i } # formula to take each number in array and add to sum variable, which creates the sum of
multiples.
puts sum # returns the sum
end
end
m = Multiples.new # create a new class
m.sum_of_multiples(m.multi_pulls) # calls the sum of the multiples
sample_array = (20.downto(1)).to_a # create the array 1 to 20
least_common_denominator = sample_array.inject(:lcm) # find the
least common denominator using using the lcm method between
each integer in the array
puts least_common_denominator
Practice Practice Practice
NewSchedule
Discussion