FAQ: Methods, Blocks, & Sorting - How Blocks Differ from Methods

This community-built FAQ covers the “How Blocks Differ from Methods” 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 How Blocks Differ from Methods

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!

In the following line;

puts "#{string[0].upcase}#{string[1..-1]}"

I don’t understand why ‘…-1’ works as it does. Can anyone explain this bit?

7 Likes

https://www.codecademy.com/en/forum_questions/5501ebfc86f552fd32004059

2 Likes

Hi, an experiment to this exercise got me baffled - there are numbers now!
can s.b. explain?

same for me, i don’t quite understand what it is referring too … is there anyone to enlight me please :slight_smile:

Using negative index numbers starts from the end of the array, rather than the beginning. As there’s no negative Zero, Array[-1] will return the final element of the array.

That line is saying, 'return the first element of the string ( string[0] ) as an upper case string, and then return every other element from the second up to and including the final element ( string[1…-1] )

15 Likes

very nice all clear now :slight_smile:
thanks a lot !!

I don’t understand why the first method has to iterate over every letter in the string. Wouldn’t it be simpler to just do this?:

method that capitalizes a word

def capitalize(string)
puts “#{string.capitalize}”
end

4 Likes

Can anyone explain to me why this example created a method to capitalize and didn’t just do

puts “#{string.capitalize!}” - it gives the exact same output as puts "#{string[0].upcase}#{string[1…-1]}

Thanks

1 Like

This really helped the most. I had no idea -1 was the end of an array.

So [0…-1] just means from the first to the last in the array?

It would be helpful to add that to the exercise description!

Just some food for thought but I was curious to see what the feedback was the -1 thing is covered a little bit earlier on… Codecademy probs designed it to keep it fresh… The -1 bit does come up in other languages and pardon my dual language (or duo lingo?? :owl:) but it is a crucial language concept in arrays and what not in general. It’s like I see you like learning languages so we put languages in your languages. :sweat_smile: