Ruby function and files - quick programs to explore language

27 Sep 2019

Notes based on Learn Ruby the hard way.

Most of the code examples are from the book. When I refactor some of them to make them better, I explain the reasons why. I remember reading this book a couple years ago and getting stuck a lot. Somewhere along the way I've picked up the ability to read them and identify a lot of the areas that feel wrong to me. This is a book for a total beginner so have been written with a lot of duplication to make the examples less overwhelming.

Function examples


def puts_two(*args)
  arg1, arg2 = args
  puts "arg1: #{arg1}, arg2: #{arg2}"
end

def puts_two_again(arg1, arg2)
  puts "arg1: #{arg1}, arg2: #{arg2}"
end

def puts_one(arg1)
  puts "arg1: #{arg1}"
end

def puts_none()
  puts "I got nothin'."
end

puts_two("Rebecca", "Williams")
puts_two_again("Rebecca", "Williams")
puts_one("First!")
puts_none()

Functions and variables


def cheese_and_crackers(cheese_count, boxes_of_crackers)
  puts "You have #{cheese_count} cheeses!"
  puts "You have #{boxes_of_crackers} boxes of cracke
rs!"
  puts "Man, that's enough for a party!"
  puts "Get a blanket."
  puts # A blank line
end

puts "We can just give the function numbers directly:
"
cheese_and_crackers(20, 30)

puts "OR, we can use variables from our script:"
amount_of_cheese = 10
amount_of_crackers = 50
cheese_and_crackers(amount_of_cheese, amount_of_crackers)

puts "We can even do math inside too:"
cheese_and_crackers(10 + 20, 5 + 6)

puts "And we can combine the two, variables and math:
"
cheese_and_crackers(amount_of_cheese + 100, amount_of_crackers + 1000)

Functions and Files


input_file = ARGV[0]

def print_all(f)
  puts f.read()
end

def rewind(f)
  f.seek(0, IO::SEEK_SET)
end

def print_a_line(line_count, f)
  puts "#{line_count} #{f.readline}"
end

current_file = File.open(input_file)

puts "First let's print the whole file:"
puts # A blank line

print_all(current_file)

puts "Now let's rewind, kind of like a tape."

rewind(current_file)

puts "let's print three lines:"

current_line = 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

current_line = current_line + 1
print_a_line(current_line, current_file)

Calculator


def add(a,b)
  puts "ADDING #{a} + #{b}"
  a + b
end

def subtract(a,b)
  puts "SUBTRACTING #{a} - #{b}"
  a - b
end

def multiply(a,b)
  puts "MULTIPLYING #{a} #{b}"
  a * b
end

def divide(a,b)
  puts "DIVIDING #{a} / #{b}"
  a / b
end

puts "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78,4)
weight = multiply(90, 2)
iq = divide(100, 2)

puts "Age: #{age}, Height: #{height}, Weight: #{weight}, IQ: #{iq}"