Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Introduction to Ruby
  • Discussion

  • Study Guides

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Methods: Part 2

  • If a block object is passed in a method with the unary ampersand operator '&', it will be treated as a Proc object
  • If a Proc object is passed in a method with the unary ampersand operator '&', it will be treated as a block object
  • Methods can have parameters with default values
  • To create it, the parameter name is followed with an equal sign and value
  • Methods can have variable-length arguments
  • To create it, add a * before the method's parameter
  • In the method, the parameter will refer to an array
  • Passing a hash as a parameter is more organized than passing multiple arguments

Methods: Part 2

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.

  • Intro 0:00
  • Methods with the Unary Ampersand Operator 0:14
    • &
    • Block to a Proc
    • Example
    • Proc object
    • Example
  • Methods with Default Values 5:54
    • Example
  • Methods with variable-Length Arguments 8:05
    • How to create it
    • Example
  • Using Hashes with Arguments 13:02
    • Multiple arguments
    • Solution
    • Example
    • Rdoc

Transcription: Methods: Part 2

Welcome back to Educator.com.0000

Today's lesson is on methods; this is the second part.0001

What I want to do for this second lesson is to go over a lot of common parameters that you will be using as part of the methods, and to give us a lot of hands-on, so you can see it in action.0011

For this one, we first want to go over the unary ampersand operator.0027

This is just this little & sign here, and it allows a block object to be treated as a Proc object.0035

So, when you have a parameter, and right before that parameter you put the & sign, it's going to treat whatever block object as a Proc object.0045

I like to call this block to proc.0056

In the previous lesson, you saw that we used this processor method; this processor method took one argument, and it passed a block.0062

This time, we're actually using that processor, but notice, it has two arguments: one is arg, and the next one is actually this unary & operator.0074

This is what is going to change that block to a Proc.0087

For this example, we call Process, and it calls this method call in it.0092

That method 'call' is going to take that block, and call it...plus pass this argument--this arg.0104

Let's do an example and see it in action.0115

Let me clear that terminal out; first, let me find it.0119

I'm going to take the exact same one from the slide, and then Process.call(arg)...0124

I have my method; it's done; and then, for this one, I'm just going to call the processor, pass in the value 10, and then I'm going to pass that block, x times 2.0132

Even in this example, my second parameter--I never actually pass it in; I put a block at the end.0152

It understands that that block is part of the parameter, and it's going to convert that to a Proc, so I can call it now.0165

At the same time, I can go ahead and change the block value, which is going to change the return result--change our parameter, and that is going to change the result, too.0179

Also, if I just do 10, notice I get a NoMethodError, because it's trying to call the method call, but it has no Proc object to call it in.0191

I could easily fix that by just putting an empty block, and it's going to have to return nil.0202

That is where I pass in my value, and you notice that whatever value I put in there, it's going to return that value back, but I'm not actually doing any processing for this example.0210

OK, so, at the same time, this unary & operator can go the other way, so you can treat a Proc object to be a block.0227

A Proc object can be treated as an ordinary block...so this is a Proc to a Block.0243

For this example, I create this Proc object using Proc.new, and this says, "Whatever this value is, I want you to square it; make that value x times x," so I call it square.0253

I'm going to use this with the Array object, using the map method.0272

I have this Array; it calls map; I pass in my Proc object with that unary & operator, and it takes these values and squares them, so I end up with 1, 4, and 9.0279

Let's go ahead and do an example; I can just do square.Proc.new(x^x), and then I can pass in an Array, take the map, and then pass in my value with that.0301

And it squares the values up.0325

The greatest thing is this dynamic: I can pass as many values as I want, and it will process all of them.0328

This is an example of our object--Proc to a block.0339

Next, we want to go over default values; I actually showed you that in the first lesson, with the basic method.0350

This default value creates a parameter, and you follow it with an equals sign and a value: that is how you create it.0360

That will create your default value.0370

If I don't pass any parameter in there, it's going to use that default value.0373

This allows a method that can take an optional number of arguments.0378

All these ones with default values--I don't need to specify what it is; it will just use a default value.0383

The example: I have this adder method; it takes two parameters, but notice, that second one has my default value.0391

It has a default value of one.0403

If I just did adder, and I just passed 2, it would return to me the value 3, because b=1 is the default value.0411

Let's go ahead and show you this in some code.0427

I'm just going to pass that same one in; it takes two parameters and adds the values up.0432

I just pass 1; it's going to do 1+1; but also, if I do adder(1,5), it's not going to use that default value.0443

Since I passed in 5 for b, it says, "OK, this has prior precedence," so it's going to do 1+5.0455

I can also just do 2--it would be 3; adder(2,2) does 4; (10,20)--it's going to add them up and be 30.0464

That is our default value.0478

Next, let's look at methods with variable-length arguments.0486

This is a very interesting parameter that allows us to put a lot of arguments in the method without specifying all of them and saying what they need to do.0494

This allows parameters that allow an arbitrary number of arguments.0508

How do I create it? You add this star before the method's parameter.0516

The arguments will be passed as an Array into the code.0524

All these arguments that you pass--when it sees that star, it's going to get that variable, and it's going to be returned as an Array.0533

Make sure to process it as an Array in that case.0541

What I do here is, I have this method can min_value; my parameter is that variable-length argument.0545

This is a variable-length argument...and it's called Numbers; it takes a length of numbers.0560

This method gets the lowest value, and it returns that.0578

Let's go ahead and just go through the code really quickly.0584

The first one does a Numbers.shift, so--this Numbers is an Array; it's going to take the first element in this Array and say, "This is the minimum value."0589

Then, it's going to go through each of the other values here, index 1+...to the end; if it finds a value that is lower than the minimum, then it's going to make that the minimum value.0601

It's going to continue on this loop until it goes through all of them, and then, now, we have found the minimum value.0620

This iterates through the Array and gets the minimum value.0628

Now, it goes through all this; min actually points to the minimum; and then, it returns that value here.0656

I want to show you this in action, so let's go ahead and do min_value; it takes a parameter of Numbers; go ahead and do that Numbers--it gets the first element in that Array.0667

Then, we're going to iterate through that Array of numbers, getting the lowest one.0685

To do that, I'm going to get min=n if whatever element that is less than what is the minimum.0693

Then, at the end, I want to return min; so that is my least value there.0701

This takes a variable length of arguments, and I don't have to worry about it as I pass; I just put a comma list in.0706

I'm going to call min_value, and I can call [1,2,3,4,5,6,7], and when I press Return--you know what that code is doing--it says 1 is the lowest.0716

I can also go through a different list, where the last value is maybe a negative sign, then put some other values in there, press Return...it says the minimum value is -5, so I know it's getting the last value and going through all of them.0730

And, it works with negative numbers, too.0751

I can also use parentheses, remember...it still works; it gets the minimum values from that.0758

That is our variable-length arguments.0771

The next one is...you will commonly say, "While I'm programming, I have so many parameters I'm passing--it's going to get disorganized; it's going to get confusing; I don't want to pass it in a certain order--I want more flexibility when I code."0778

That is the reason we should use Hashes--multiple arguments that get disorganized, that get difficult to remember in the proper order.0795

I just want to work the code and get it processed.0807

Solution: pass a Hash as a parameter.0809

Depending on what you're doing, you will want to have this flexibility.0815

You might want to force it to have a certain amount of order in the primers, but in this case, we're being flexible; we're just saying that we're going to use that Hash; we're going to process it as a Hash.0821

Here in my example, I have this method called multiplier; it takes Args, which is a Hash.0833

In my code, I actually didn't default it to a Hash, but I can make a default value a Hash here, too.0848

It's going to take four keys in the Hash and multiply them together; that is why it's called multiplier; it multiplies four elements.0857

Look at my code; notice, it actually looks for a symbol called key 1.0868

If that key doesn't exist in the Hash, it's going to default it to value 1.0874

The same for key 2; if it doesn't exist, it's going to default it to value 2; the same as 3 and 4.0879

At the end result, it's going to multiply them together and return that value.0888

Let's make this example in our code.0895

Let's go ahead and add the parentheses this time, since we haven't been using that.0901

I'm going to go ahead and put in all the key values; notice that these are not String keys; these are Symbols.0910

I'll make it easier, just substituting the number...so, I have all my values here, and then I just want to say, "Just multiply them all together."0924

It's going to get those four values, multiply them together, and return it.0943

Now, when I call multiplier, I just have an empty Hash; it says wrong number of arguments (0 for 1); let's see, if I put those parentheses, if that fixes it...it does!0949

With a Hash, make sure you have parentheses.0963

Notice, I pass an empty Hash in there; all that is going to do is say, "You passed in a Hash; you didn't give me the indication to define them in there; I'm just going to use the default I've already defined in the code: 1 times 2 times 3 times 4, and that's going to be 24."0968

I can also define a couple of those values; so that key, 1, I make that 2, and that key 2--I'm going to make that 3.0986

This is going to be 2 times 3 times 3 times 4, and I get 72.1003

I actually can define them all; I'm going to define them all as 1, so my value should be 1, too.1010

My value is 1: 1 times 1 times 1 times 1.1025

Notice, I'm passing a bunch of different values--some of them I don't even define, but it's allowing me to do it, because I'm using this Hash, and in my code, I tell it these default values that I can specify.1029

For this one, it's 5 times 2 times 3 times 4; it's 120.1051

Remember, I can still pass in other parameters to this.1062

I have this Hash, but I can also pass in other parameters after this, and I can still process it, also.1065

Depending on what you're doing...I just want you to get the concept down.1072

That is using Hashes with arguments.1080

Let's quickly go over the RDoc for the method.1083

It's very simple; it has method, and it has Proc, because they are so closely integrated.1093

Proc to block, block to Proc...we talked about that...1103

Notice here, it does have some methods to check for equality: "Two method objects are equal if they are bound to the same object and refer to the same method definition."1107

You already saw the call method in action--"Invokes the meth with specified arguments, returning the method's return value."1116

There is a method called arity--"Returns an indication of the number of arguments accepted by a method."1128

You already saw the call method...1135

You can also use inspect or to_s to return "the name of the underlying method."1140

This is interesting; you can also call parameters, and it "returns the parameter information" of that method.1148

Receiver "returns the bound receiver of the method object."1156

We already talked in a previous lesson about unbind--"Dissociates meth from its current receiver."1160

Let's see that in action--this parameters.1169

So, multiplier.parameters...wrong number of arguments...it's probably not being used in the right way...1176

Anyway, I will let you look deeper into that RDoc.1196

That is the end of the methods lesson, part 2 of 2.1202

See you again at Educator.com!1208