[tcortina@linux3 ~]$ cd private [tcortina@linux3 ~]$ mkdir 15110 [tcortina@linux3 ~/private]$ ls 15110 [tcortina@linux3 ~/private]$ cd 15110 [tcortina@linux3 ~/private]$ mkdir lab2 [tcortina@linux3 ~/private]$ mkdir practice [tcortina@linux3 15110]$ ls lab2 practice [tcortina@linux3 15110]$ cd practice [tcortina@linxu3 practice]$ pwd /afs/cs.cmu.edu/user/tcortina/private/15110/practice [tcortina@linux3 practice]$ irb irb(main):001:0> 15 + 110 => 125 irb(main):002:0> 15 - 110 => -95 irb(main):003:0> 15 * 110 => 1650 irb(main):004:0> 15 / 110 => 0 irb(main):005:0> 15.0 / 110.0 => 0.136363636363636 irb(main):006:0> 15.0 / 110 => 0.136363636363636 irb(main):007:0> 99 / 100 => 0 irb(main):008:0> 1.0 / 3.0 => 0.333333333333333 irb(main):009:0> 2.0 / 3.0 => 0.666666666666667 irb(main):010:0> 7.0 / 9.0 => 0.777777777777778 irb(main):011:0> 110 % 15 => 5 irb(main):012:0> 42 % 18 => 6 irb(main):013:0> 42 % 6 => 0 irb(main):014:0> 17 % 2 => 1 irb(main):015:0> 18 % 2 => 0 irb(main):016:0> 19 % 2 => 1 irb(main):017:0> 20 % 2 => 0 irb(main):018:0> 15 % 110 => 15 irb(main):019:0> 5 ** 3 => 125 irb(main):020:0> 8 ** 2.4 => 147.03338943962 irb(main):021:0> 9 ** 0.5 => 3.0 irb(main):022:0> 9 ** (1/2) => 1 irb(main):023:0> 27 ** (1.0/3.0) => 3.0 irb(main):024:0> 5 * 6 + 7 => 37 irb(main):025:0> 5 * (6 + 7) => 65 irb(main):026:0> 3 * 4 / 5 => 2 irb(main):027:0> 3 * (4 / 5) => 0 irb(main):028:0> 2 ** 3 ** 4 => 2417851639229258349412352 irb(main):029:0> (2 ** 3) ** 4 => 4096 irb(main):030:0> x = 6 => 6 irb(main):031:0> y = 14.5 => 14.5 irb(main):032:0> z = 42 % 18 => 6 irb(main):033:0> x => 6 irb(main):034:0> y => 14.5 irb(main):035:0> x + z => 12 irb(main):036:0> gravity = 9.8 => 9.8 irb(main):037:0> interest_rate = 0.015 => 0.015 irb(main):038:0> 7up = 2.39 SyntaxError: compile error (irb):38: syntax error 7up = 2.39 ^ from (irb):38 from :0 irb(main):039:0> courseFall2011 = 15110 => 15110 irb(main):040:0> gravity => 9.8 irb(main):041:0> gravitY NameError: undefined local variable or method `gravitY' for main:Object from (irb):41 from :0 irb(main):042:0> base = 15 => 15 irb(main):043:0> height = 110 => 110 irb(main):044:0> triangle_area = 0.5 * base * height => 825.0 irb(main):045:0> base = 20 => 20 irb(main):046:0> triangle_area = 0.5 * base * height => 1100.0 irb(main):047:0> base = 25 => 25 irb(main):048:0> triangle_area => 1100.0 irb(main):049:0> triangle_area = 0.5 * base * height => 1375.0 irb(main):050:0> course = 15110 => 15110 irb(main):051:0> course + 2 => 15112 irb(main):052:0> course = "15110" => "15110" irb(main):053:0> course + 2 TypeError: can't convert Fixnum into String from (irb):53:in `+' from (irb):53 from :0 irb(main):054:0> s1 = " " => " " irb(main):055:0> s2 = "" => "" irb(main):056:0> male = true => true irb(main):057:0> married = false => false irb(main):058:0> f = 32 => 32 irb(main):059:0> c = 5/9 * (f-32) => 0 irb(main):060:0> f = 80 => 80 irb(main):061:0> c = 5/9 * (f-32) => 0 irb(main):062:0> c = (5.0/9.0) * (f-32) => 26.6666666666667 irb(main):063:0> side = 109 => 109 irb(main):064:0> square = side * side => 11881 irb(main):065:0> triangle = 0.5 * side / 2 * side / 2 => 1485.125 irb(main):066:0> area = square - triangle => 10395.875 At this point, I use gedit to create a Ruby file that contained one function that computes the area of a countertop that is in the shape of a square: def compute_area(side) area = side * side return area end By saving the file as "countertop.rb", keywords are highlighted in red. Constants in pink. In this function definition, there is one parameter, side. When we call this function, we need to supply one value for the side. It returns the answer back to wherever we call the function (in this case, irb). In irb again: irb(main):067:0> load "countertop.rb" => true irb(main):068:0> compute_area(15) => 225 irb(main):069:0> compute_area(110) => 12100 irb(main):070:0> x = 42 => 42 irb(main):071:0> compute_area(x) => 1764 irb(main):072:0> compute_area(15) + compute_area(110) => 12325 Editing countertop.rb, we replace the return statement with a print statement. def compute_area(side) area = side * side print area, "\n" end The function now prints its answer along with a newline ("\n") and returns nothing to wherever we call the function. So when we call the function now, the function prints the answer but irb displays "nil" since it got nothing back from the function. This is fine unless we want to use the answer in another computation as shown below. irb(main):073:0> load "countertop.rb" => true irb(main):074:0> compute_area(15) 225 => nil irb(main):075:0> compute_area(110) 12100 => nil irb(main):076:0> compute_area(15) + compute_area(110) 225 12100 NoMethodError: undefined method `+' for nil:NilClass from (irb):6 from :0 The reason the previous expression fails is that each call prints out its own answer, but neither answer is returned back to irb (since there is no return statement) so irb can't add the computed results together. Using the Math module: irb(main):077:0> Math.sqrt(16) => 4.0 irb(main):078:0> Math.log10(100.0) => 2.0 irb(main):079:0> Math::PI => 3.14159265358979 irb(main):080:0> Math.sin(Math::PI / 2) => 1.0 irb(main):081:0> include Math => Object irb(main):082:0> sqrt(16) => 4.0 irb(main):083:0> PI => 3.14159265358979 irb(main):084:0> quit