Functions and Files

input_file = ARGV.first     #the first file name is typed in is input_file

 

def print_all(f)    #f in this case is a variable but also a file

puts f.read

end

def rewind(f)

f.seek(0)    #f.seek(0) moves to the start of the file >> to 0 bytes of the file

end

def print_a_line(line_count, f)

puts “#{line_count}, #{f.gets.chomp}”    #i guess f.gets.chomp knows on which line we                                                                                           are at

end

 

current_file = open(input_file)   #open the initial file and assign it to a variable (current_file)

puts “First let’s print the whole file:\n”

print_all(current_file)   #runs the first function, reads and prints it

puts “Now let’s rewind, kind of like a tape.”

rewind(current_file)   #now moves to the start of current file

puts “Let’s print three lines:”

 

current_line = 1    #passes a temporary value to the variable

print_a_line(current_line, current_file)    #now runs the third function and this is                                                                                               the moment current_line = line.count

current_line = current_line + 1

print_a_line(current_line, current_file)

 

current_line = current_line + 1

print_a_line(current_line, current_file)

 

The result should’ve looked like this:

ruby ex20.rb test.txt
First let’s print the whole file:
This is line 1
This is line 2
This is line 3
Now let’s rewind, kind of like a tape.
Let’s print three lines:
1, This is line 1
2, This is line 2
3. This is line 3

to make current_line = current_line + 1 more compact we can use “+=” it works like this:

x = x + y   <=>  x += y

it would look like this:

current_line += 1

print_a_line(current_line, current_file)

 

 

 

Functions can return something

 

def add(a, b)

puts “ADDING #{a} + #{b}”

return a + b

end

 

def substract(a, b)

puts “SUBSTRACTING #{a} – #{b}”

return a – b

end

 

def multiply(a, b)

puts “MULTIPLYING #{a} * #{b}”

return a * b

end

 

def divide(a, b)

puts “DIVIDE #{a} / #{b}”

return a / b

end

 

puts “Let’s do some math with just functions!”

age = add(30,5)

height = substract(78, 4)

weight = multiply(90, 2)

iq = divide(100, 2)

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

 

# A puzzle for the extra credit 

puts “Here’s a puzzle”

what = add(age, substract(height, multiply(weight, divide(iq, 2))))

puts “That becomes: #{what}. Can you do it by hand?”   #no i couldnt 😀

 

Return: after running our functions with 2 arguments like for add(30,5) we can then assign result to a variable, it basically saves/returns the result to us.

 

Back to the puzzle:

what = add(age, substract(height, multiply(weight, divide(iq, 2))))

 

what1 = divide(iq/2)  => 25

what2 = multiply(weight*25) => 4500

what3 = substract(height-4500) => -4426

what = add(age+(-4426)) => -4391

 

We took the result of one function and put it as an argument into another 🙂

 

 

Practice what I’ve learned so far

 

puts “Let’s practice everything.”
puts ‘You\’d need to know \’bout escapes with \\ that do \n newlines and t\ tabs’

#\’ to include ” ‘ “, \ to include “\” , \n to skip to a new line t\ to tab in

 

# the <<END is a “heredoc”. See the Student Questions

poem = <<END

\tThe lovely world

with logic so firmly planted

cannot discern \n the needs of love

nor comprehend passion from intuition

and requires an explanation

\n\t\twhere there is none.

END

 

puts “————–”

puts poem

puts “————–”

 

five = 10 – 2 + 3 – 6

puts “this should be five: #{five}”

 

def secret_formula(started)

jelly_beans = started * 500

jars = jelly_beans / 1000

crates = jars / 100

return jelly_beans, jars, crates

end

 

start_point = 10000

beans, jars, crates = secret_formula(start_point)

puts “With a starting point of: #{start_point}”

puts “We’d have #{beans} beans, #{jars} jars, and #{crates} crates.”

 

start_point = start_point / 10    #start_point has a another value now

puts “We can also do that this way:”

puts “We’d have %s beans, %d jars, and %d crates.” % secret_formula(start_point)

 

<<END: That is called a “heredoc” or “here document”. It is used to create a multi-line string, and you use it by starting with << and an all caps word, in this case END.

 

%s , %d , %d : It’s a C way of including variables into Ruby strings.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s