Chapter 5: Fruitful Methods

5.1 Return values

#!/usr/bin/env ruby

e = Math.exp(1.0)
angle = Math::PI / 4; radius = 1;
height = radius * Math.sin(angle)
#puts e, height

########################################

def area(radius)
  a = Math::PI * radius * radius
  return a
end

########################################

def area(radius)
  return Math::PI * radius * radius
end

#puts area(1)
#puts area(2)

########################################

def absolute_value(x)
  if x < 0
    return -x
  else
    return x
  end
end

########################################

def absolute_value(x)
  if x < 0
    return -x
  elsif x > 0
    return x
  end
end

puts absolute_value(3)
puts absolute_value(-3)
puts absolute_value(0) # returns nil, no warnings

5.2 Program development

#!/usr/bin/env ruby

def distance(x1, y1, x2, y2)
  return 0.0
end

########################################

dist = distance(1.0, 2.0, 4.0, 6.0)

########################################

def distance(x1, y1, x2, y2)
  dx = x2 - x1
  dy = y2 - y1
  puts "dx is " + dx.to_s
  puts "dy is " + dy.to_s
  return 0.0
end

puts distance(1.0, 2.0, 4.0, 6.0)

########################################

def distance(x1, y1, x2, y2)
  dx = x2 - x1
  dy = y2 - y1
  dsquared = dx*dx + dy*dy
  puts "dsquared is " + dsquared.to_s
  return 0.0
end

puts distance(1.0, 2.0, 4.0, 6.0)

########################################

def distance(x1, y1, x2, y2)
  dx = x2 - x1
  dy = y2 - y1
  dsquared = dx*dx + dy*dy
  result = Math.sqrt(dsquared)
  return result
end

puts distance(1.0, 2.0, 4.0, 6.0)

5.3 Composition

#!/usr/bin/env ruby

require 'jch05.01.rb'
require 'jch05.02.rb'

def fred(xc, yc, xp, yp)
  r = distance(xc, yc, xp, yp)
  a = area(r)
  return a
end

########################################

def fred(xc, yc, xp, yp)
  return area(distance(xc, yc, xp, yp))
end

#puts fred(1.0, 2.0, 4.0, 6.0)

5.4 Overloading

5.5 Boolean expressions

#!/usr/bin/env ruby

fred = true
test_result = false

########################################

n = 4; x = 3;
even_flag = (n % 2 == 0) # true if n is even
positive_flag = (x > 0) # true if x is positive

########################################

if even_flag
  puts "n was even when I checked it"
end

5.6 Logical operators

#!/usr/bin/env ruby

x = 6
if x > 0
  if x < 10
    puts "x is a positive single digit."
  end
end

5.7 Boolean methods

#!/usr/bin/env ruby

def single_digit?(x)
  if x >= 0 and x < 10
    return true
  else
    return false
  end
end

########################################

def single_digit?(x)
  return (x >= 0 and x < 10)
end

########################################

big_flag = !single_digit?(17)
puts single_digit?(2)

########################################

x = 6
if single_digit?(x)
  puts "x is little"
else
  puts "x is big"
end

5.8 More recursion

#!/usr/bin/env ruby

def factorial(n)
end

########################################

def factorial(n)
  if n == 0
    return 1;
  end
end

########################################

def factorial(n)
  if n == 0
    return 1;
  else
    recurse = factorial(n-1)
    result = n * recurse
    return result
  end
end

puts factorial(33) # machine unhappy for n > 33

5.9 Leap of faith

5.10 One more example

#!/usr/bin/env ruby

def factorial(n)
  if n == 0
    return 1
  else
    return n * factorial(n-1)
  end
end

puts factorial(6)

########################################

def fibonacci(n)
  if n == 0 or n == 1
    return 1;
  else
    return fibonacci(n-1) + fibonacci(n-2)
  end
end

puts fibonacci(6)

5.11 Glossary

5.12 Exercises

GNU Free Documentation License Valid HTML Valid CSS Built with BBEdit
Friendly links for Google “juice”