Learn Ruby
Instance Method
20.5
Lesson

Biscuit as a gift

Let’s say that you have two friends “Mike” and “Adam”. You want to make two biscuits for them and you want to gift them the biscuit.

Run the code and we will see that both the biscuits output the same message Mike this biscuit is a gift for you. We want the second message to be for Adam. Second message should have been Adam this biscuit is a gift for you.

To solve this problem we need the name of the person to be a variable. In Ruby we can pass values to methods.

First let’s see how we solve this problem then we will discuss it. We are removing the eat method since that’s not important for now.

First let’s see how we are calling the method gift.

# iniitally
biscuit1.gift

# now
biscuit1.gift("Mike")

Now when we are calling the method gift then we are also passing the name of the person for whom the gift is.

Let’s see what has changed on the method side.

# initially
def gift
  ...
end

# now
def gift(name)
  ...
end

Here we have declared a variable called name.

When biscuit1.gift("Mike") is called then name gets the value “Mike”.

Similarly when biscuit2.gift("Adam") is called then name gets the value “Adam”.

I know all this is confusing. We will go over these concepts again in the next lesson.