Introduction to Ruby Methods: Part 1

In this lesson, our instructor talks about methods. First, he discusses what is a method, basic method, return value, factorial, return two values, and undefining methods. Then he talks about method names, method names with question mark, method names with exclamation point, and method aliases. Lastly, he lectures on operator methods, parentheses, and blocks.

Methods: Part 1

A method provides a way to gather statements and expressions into one area

It can be used repeatedly to execute a specific task

It can be defined to do all sorts of tasks

The return keyword forces a return to the end of the method

Any value following the return value will also be returned

The method undef allows you to undefine instance methods

Method names should begin with a lowercase letter

When longer than a word, it should be separated with underscores

A method name with a question mark answers the question posed by the method

Usually it returns true or false

A method name with an exclamation point is usually a mutator method

The exclamation point means to use it with caution

Method aliases allow you to define a new name to an existing method

Operator methods can be defined in your own class

Methods can be associated with code blocks

The method block_given? will return true if a block is passed

Methods: Part 1

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

Right here, I have a factorial example; this will give you the factorial, depending on the parameter put in.0375

See here, our method factorial takes one argument; it returns 1 if you pass y=0.0384

Notice that it just returns: that means that it ignores this next line; so if y=0, it's going to ignore this line over here, but if it is 1 or greater, then it's going to call this method here.0395

This method actually calls itself; so, if I did 3, it would do 3 times factorial...and 3-1, we have a 2, and after that, it calls factorial of 2...that will get you 2...times factorial 1.0413

Here is where we get that 1; this is going to break down, 1 times factorial 0, and notice, when it hits 0, it sees that over here: it says, "OK, that y value is equal to 0--I'm just going to return 1."--so that is 1.0453

Now that we have broken down what all the different factorials are, here is what it gets when I pass factorial 3--remember, this is for factorial 3: when I do that, it's going to do 3 times 2 times 1 times 1, and we get, at the end, 6.0476

Let's go through some code of this example; I want to show you what you can do without the return value, too.0511

Again, I'm putting one parameter there: I'm going to return 1 if that y=0, but instead of having that second return value--it's not needed, since it's the last line of this method--I'm not going to do it.0531

I'm just going to put return, y times factorial, y minus 1, and then I'm just going to end it.0548

Let's see it in action: if I do factorial 0, I get 1; factorial 1, I get 1 also; 2--that is 2 times 1--I get 2; 3--which is 3 times 2 times 1--6; 4--we get 24; and 5--120.0562

So, if you are in a class, and you are going to undefine these instance methods, you can also undefine the inherited methods.0727

For those previous examples, if I do undef(square_it), undef(factorial), it does not exist anymore; if I call those functions again, it's just going to give me an error.0735

I can actually go to the terminal...and we still have the factorial in memory, so if I do factorial(5), I get 120.0748

Now, if I call undef(factorial), and then I call it again, now it says "NameError: undefined local variable or method 'factorial' for main:Object" since I have created this method in the global scope.0757

This will do the exact opposite: whatever that other_value is, it's going to remove it from the string.1336

To do that in my code, I'm going to use a method called gsub; it's going to update the value in place.1342

I'm going to pass Regexp here, so it's going to first look for that other_value, look for any spaces around that value, remove any spaces around it...and then, it's going to return it as that value.1356

If I take my object, and I do at...and I'm going to say it's SuperString, but it has 'goop' in it now...so SC plus that string value...it's going to use this operator here...and the return value is 'SuperString with goop', which is my new value in that object.1483

The same thing--I can subtract it out, so if I do SC minus 'goop', notice it removes that whole word.1510

So, now it's 'SuperString with'; that doesn't sound that good, so I want to remove that 'with', also.1519

Now, it says 'SuperString'; let's say 'with SuperOperators', since we have the new binary operators.1526

I have this value 'Hi', and when I call the link method, which is part of this string, it will return me the number of characters.1611

But notice, I can return it with parentheses or none; both of them will still work.1620

There are cases where it is required, though, so let's go over those.1632

The main one is method invocations with more than one argument; this is a case where it is required.1637

For example, I have a method that takes two arguments; if I don't put these parentheses, it's going to show me a warning that says, "That's not proper; I don't know if it's being used for a method or something else."1647

So this block--now, using this yield command, you can actually tell it when to call it.1719

I have this method called processor: it takes one value, a number value.1725

Now, if a block is given, I'm going to say, "Process it using that block; I don't know what that is, but process it with it--you must know what you want to process it for if you have the block there; else, I'm going to process it myself."1735

I'm just going to multiply whatever that parameter is by itself; so it's num squared.1750