This community-built FAQ covers the “The ‘.length’ Method” exercise from the lesson “Introduction to Ruby”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Learn Ruby
FAQs on the exercise The ‘.length’ Method
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 ( ) 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 ( ) below!
Agree with a comment or answer? 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 !
How do you puts the answer to a .length method?
2 Likes
mtf
November 7, 2018, 11:16pm
4
Precede the method call with a puts
…
puts "Wee Gillis".length # 10
The method will be evaluated, then printed.
5 Likes
The instructions for the exercise say,
Call the .length
method on your name (remember to use quotes around your name).
What does it mean to “call” something? As in on my phone? I’m so confused!
mtf
March 4, 2019, 8:22pm
6
Given a method…
def my_method(x)
x.to_s
end
puts my_method(42) # <- call the method on 42
.length
is a method of string and array objects. Given a string,
"string".length
Given an array,
[1,2,3,4,5,6,7,8,9].length
print “abc =” “abc”.length
prints 8, how is this correct ?
1 Like
mtf
October 12, 2019, 6:16pm
11
Ruby would appear to concatenate two strings when given in this manner…
print "abc =" "abc".length
which becomes,
print "abc =" + "abc".length
and,
print "abc =abc".length
which has length 8.
Ruby does a few things implicitly, such as return from methods, and now it would seem concatenate, above. It’s new to me, but I’m not surprised.
The language is fairly easy to learn, but I’m told not for casual learners. To learn Ruby, one must be all in or go for another language.
Hi, why is it that it asks us to put [puts] instead of [print] in this excercise, if ultimately I just want it to take the length of the string and print it to the screen? I’ve tried both and they both show the number so, is there any specific reason or in this case it is just not that important which one we use?
mtf
December 10, 2019, 8:23pm
13
The subtle difference between the two is,
puts => (PUT String) includes a newline character
print => does not include a newline, pencil stays where it is
(1..6).each { |x| puts x }
1
2
3
4
5
6
(1..6).each { |x| print x }
123456
1 Like
why is not the result showing on my console
1 Like
mtf
June 10, 2020, 12:00pm
15
Can you give us an example, perhaps a screen grab?
Hi,
I don’t understand why in the example “I love espresso”.length it is not necessary to use puts before it to find the number 15 (length).
While when I tried to do it with “Fernanda”.length I could not see my word count until I wrote
puts “Fernanda”.length
Why couldn’t I run the code on my console without puts, if in the previous exercises I didn’t need puts to run it?
Besides, I am also confused with the use of puts here, as puts is supposed to give you a new (blank) line, right?
mtf
November 7, 2020, 7:12pm
18
`puts` => put string
Will output a string to the console AND a newline. If the string is empty then it will still output the newline.
`print` => print string
will output a string to the console without a newline so the draw pencil stays where the string ends. The next print
will begin from that point.
Nothing appears in the console if we don’t puts
or print
it. There is no interactive console in the CC Learning Environment.
I typed in a 16 digit fake credit card number and this exercise said it was 19 digits!!?
mtf
June 1, 2021, 2:17am
20
Consider the space characters. They are included in the length count.
Finding the digits is our next problem.