Return of Day 18: Still Learning Ruby the Hard Way

FYI, I am going to try the Atom Text Editor tonight. I have so far tried Sublime and Notepad++. I am wanting the perfect editor!

Exercise 18: Names, Variables, Code, Functions

So, functions. Mr. Shaw says they do 3 things, which I will paraphrase:

1. They name pieces of code
2. They take arguments the same way the ARGV does
3. Using 1 and 2 they let you make your own mini-scripts

You make them using def in Ruby.

Here is the code I type for this lesson:

And the result:

PS C:\ruby> ruby ex18.rb
arg1: Zed, arg2 Shaw
arg1: Zed, arg2: Shaw
arg1: First!
I got nothin’.

So, I want to look specifically at the new info for me. This *args is new… It is described as being like the ARGV but for functions.

The second function shows a simpler way to do the first one, by making a hybrid of the two lines from above.

Study Drills

We are going to make a function checklist!

__ Start function with def
__ Function name must only have characters and an _ character
__ Put an ( right after the function name
__ Put your arguments after the open parenthesis
__ Make each argument unique
__ Close the parenthesis
__ Indent all the lines of code in the function 2 spaces
__ End the function with end

When you run the function, here is another checklist:

__ Call the function by typing its name
__ Put the ( character after the name to run it
__ Put the values you want into the parenthesis, separated by commas
__ End the function call with a )
__ If there are no parameters in the function, you don’t need a () but it does make it clearer

OK, I am exhausted/ I will start tomorrow with: Exercise 19: Functions and Variables