FAQ: Methods, Blocks, & Sorting - Call It!

This community-built FAQ covers the “Call It!” exercise from the lesson “Methods, Blocks, & Sorting”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Call It!

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can i get some examples of “call it” please?

ex.

def array_of_10
puts (1…10).to_a
end

Call to method…

array_of_10()

Output

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I can never remember which range operator is inclusive and which is exclusive. Above the assumption is ... is inclusive.


Edit

Gets me every time. I had a hunch that ... is exclusive, and .. is inclusive. The output above should read,

[1, 2, 3, 4, 5, 6, 7, 8, 9]

To include the 10, use

puts (1..10).to_a
2 Likes

THANK YOU SOOO MUCH!!! You are a code savior!! I have tried both ways with “...” and “..”!

2 Likes

You’re welcome!

Another gotcha that made me go back and check…

def array_of_10

The above signature line has no parameter. That means we can call it without an argument list…

array_of_10

This Ruby stuff may be fine for children, but us grown-ups are always on the hook where those binary decisions have to be made. (LOL)

What’s really neat about this is we store objects in methods, rather than giving them variables.

def array_of_10
    (1..10).to_a
end

puts array_of_10
[1, 2, 3, 4, 5, 6, 7, 8, 9]

And getting into more fun stuff…

puts array_of_10.map {|x| x * 2}

Output

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

The array_of_10 method is truly a reusable object in every regard. And nary an assignment.

1 Like

I put:

def array_of_10
puts (1…10).to_a
end

puts array_of_10

but the result was:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
40

Why is the 40 there?
And should it be
puts_array_of_10
or
puts array_of_10

array_of_10 is a method. Since you are printing the return, then the method does not need to print also.

def array_of_10
  (1..10).to_a
end

puts array_of_10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

As to why 40 appears, that bears some more investigation.

The above example code shows there is white space following the keyword, puts.

puts can be translated to ‘put string’, and is easy to record in your mind. When you see, puts, your mind will say, put string.


Still unclear where the 40 comes from, but I have reproduced it. If we run only this code,

def array_of_10
  puts (1..10).to_a
end

nothing is printed to the console.

Thanks for getting back to me so quickly!

The editor already contained:

def array_of_10
puts (1…10).to_a
end

Here were the instructions:

We’ve set up a function, array_of_10 , in the editor to the right. Call it on line 5!

If they have already called the method on line 2 then why would they ask to call it again on line 5?

And I still don’t get why 40 would appear. If I call it twice then wouldn’t the output just appear again on a second line?

Consider the reasoning of the earlier example…

def array_of_10
  (1..10).to_a
end

We have essentially defined an object which can be iterated. The memory footprint is very small since we don’t preserve the data structure if it is never assigned.

How big will be the footprint of,

puts array_of_10.map {|x| x * 2}

?

Answer? 0.

There is nothing stored in memory.

We soon learn to make great use of this methodology in Ruby since it is largely, or wholly based upon methods.

Sorry I didn’t understand your answer and how it is responding to my question of why we need to call it twice with 2 puts

And also why the 40 appears ?

Upon resetting and immediately running the provided code, nothing appears in the console. There is only one puts, which is written in the method.

The correction for this is to remove the puts from the method and print the method, which still amounts to one puts. We’ve covered this above. Study that answer if you don’t understand it. Due diligence would involve Ruby docs.

As for the 40, I’m still out in woods on that one.

So if I am understanding correctly, having the puts written in the method does not actually print on the console. That is why we have to remove it from the method that the program provided and call it in a different line. Is that correct?