Skip to content

Instantly share code, notes, and snippets.

@bjgaynor
Created September 9, 2013 09:48
Show Gist options
  • Save bjgaynor/6493605 to your computer and use it in GitHub Desktop.
Save bjgaynor/6493605 to your computer and use it in GitHub Desktop.
#1
home_address = "416 Tenth Street"
home_city = "Pittsburgh"
home_state = "Pennsylvania"
#2
first_name = "Anne"
#this give first_name, a category, an actual name to be used. It is much like putting something in a box.
first_name == "Anne"
#the double equals sign just checks to see if both sides of it are equal, responding with True or False.
"Spalding" = first_name
#this is an invalid statement. this is like trying to put something into a box before you have the box.
#you can't assign a value to the variable before you have the variable. The computer reads left to right.
"Spalding" == first_name
#This is similar to the second line, it is checking the equality of the value "Spalding" and the variable
#first_name
#3
#"best"
#index 0
#length is 7
#4
def product(ary)
val = 1
ary.each do |x|
val *= x
end
return val
end
#5
def product_odd(ary)
val = 1
ary.each do |x|
if (val % 2) != 0
val *= x
end
end
return val
end
#6
def fizzblam
(1..100).each do |number|
if (number % 5 == 0) && (number % 7 == 0)
print number.to_s() + " FizzBlam "
elsif number % 5 == 0
print number.to_s() + " Fizz "
elsif number % 7 == 0
print number.to_s() + " Blam "
else
print number
end #end def
end #end each
end #end if
#7
#method_1 This method returns the highest value from the array.
#method_2 This method adds positive integers to the array and returns it.
#method_3 This method adds even integers to the array and returns it.
#refactored method_3
def method_3(array)
array.select {|integer| integer % 2 == 0}
end
puts method_3([1,2])
#8
#puts displays/prints the value
#return sends back a value to the calling piece of code
#9 time--7:04 minutes
#Findit method takes the unsorted array full of 9999 numbers and compares them to the numbers 1-10000.
#While comparing, it checks to see that all of the numbers from the unsorted array are included.
#This function searches for one number that is in 1-10000 and is not in the unsorted array.
#it then displays that specific number.
def method_find_it(unsorted_array)
(1..10000.each do |number|
if (!unsorted_array.include? number)
return number
end
end
end
#10 time--13:45 minutes
#psycode def method_1
#variable = gets.chomp for input
#empty_array = the grocery list
#Assign values to the variable(s)
#user input either = 1, ends program, or += 1
#to allow the list of items to grow in quantity
#if statement for this
#puts groceries
def Groc_list
groceries = {}
while true
puts "Enter an item, type 'exit' to exit"
item_to_add = gets.chomp
if item_to_add == 'exit'
break
end
if groceries.include.include? item_to_add
groceries[item_to_add] += 1
else
groceries[item_to_add] = 1
end
puts groceries
end
end
#11 time--34:54 minutes
class House
def initialize(current_temperature, heat, air)
@current_temperature = current_temperature
@heat = heat
@air = air
end
def update_temperature!
if @heat
@current_temperature += 1
puts @current_temperature
elsif @air
@current_temperature -= 2
puts @current_temperature
end
end
end
my_house = House.new(70, false, true)
my_house.update_temperature!
#12
#http://jsfiddle.net/faAvr/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment