This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def largest_integer(array) | |
i=0 | |
if array.length > i | |
return (array.max) | |
else | |
return nil | |
end | |
end | |
puts largest_integer([]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def welcome(address) | |
if welcome == "CA" | |
return "Welcome to California" | |
else | |
return "You should move to California" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(num1, num2) | |
puts num3 = num1 + num2 | |
return | |
end | |
add(3, 4) | |
def subtract(num1, num2) | |
puts num3 = num1 - num2 | |
return | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def total = [ 1,2,3 ] | |
return total.sum | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_roman(num) | |
words='' | |
romans = { | |
1000=>'M', | |
900=>'CM', | |
500=>'D', | |
400=>'CD', | |
100=>'C', | |
90=>'XC', | |
50=>'L', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
@@WHEELS = 4 | |
def initialize(args) | |
@color = args[:color] | |
@wheels = @@WHEELS | |
end | |
def drive | |
@status = :driving | |
end | |
def brake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |