Skip to content

Instantly share code, notes, and snippets.

View mehagel's full-sized avatar

Mark E Hagel mehagel

  • Melissa Data
  • Canby, OR
View GitHub Profile
def largest_integer(array)
i=0
if array.length > i
return (array.max)
else
return nil
end
end
puts largest_integer([])
def welcome(address)
if welcome == "CA"
return "Welcome to California"
else
return "You should move to California"
end
end
def add(num1, num2)
puts num3 = num1 + num2
return
end
add(3, 4)
def subtract(num1, num2)
puts num3 = num1 - num2
return
end
def total = [ 1,2,3 ]
return total.sum
end
def valid_triangle?(a, b, c)
s = (a + b + c) / 2.0
# the following must be positive to be a valid triangle
ok = (s - a) * (s - b) * (s - c)
if a <= 0 || b <= 0 || c <= 0 || ok <= 0 then
raise TriangleError
end
class Die
def initialize(sides = 6)
@sides = sides
end
# Remember: rand(N) randomly returns one of N consecutive integers, starting at 0
# So rand(N) returns a random integer in (0..N-1)
# And 1 + rand(N) returns a random integer in (1..N)
# See: http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-rand
def roll
def to_roman(num)
words=''
romans = {
1000=>'M',
900=>'CM',
500=>'D',
400=>'CD',
100=>'C',
90=>'XC',
50=>'L',
class Car
@@WHEELS = 4
def initialize(args)
@color = args[:color]
@wheels = @@WHEELS
end
def drive
@status = :driving
end
def brake
@mehagel
mehagel / p3_nested_array.rb
Created June 13, 2013 05:38
p3 Nested Array
def tic_tac_toe
a=["x", "o", "x", "o", "x", "x", "o", "x", "o"]
1.upto(10) do
tic_tac_toe = a.sample(9)
p board_grid = Array.new(3) { tic_tac_toe.shift(3) }
end
end
p tic_tac_toe
@mehagel
mehagel / gist:5892147
Created June 29, 2013 18:27
flow control
flow control# uncomment each of the following comments individually
# to see how break, next, and return affect loop control flow.
def looper
i = 0
while i < 20
i += 1
break if i == 9 #"Print out to the screen 1..8 than done with loop followed by HA"
# next if i.even? # "Prints out to the screen odd number between 1..19 than HA! then prints DONE!"
# return if i == 9 # "Print out to the screen 1..8 than done with loop followed by HA" The same as line 8