FAQ: Methods, Blocks, & Sorting - Practice Makes Perfect

This community-built FAQ covers the “Practice Makes Perfect” 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 Practice Makes Perfect

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!

These lessons keep using phrases like “define a method that takes two arguments” and “define a method that takes a parameter called …” interchangeably.

I think this confuses the idea of what is a parameter and what is an argument

For beginners, it should be more consistent. I know that a method ‘has’ parameters, and ‘receives’ arguments. But the lessons are getting them mixed up and this could confuse beginners.

12 Likes

So I understand most of method one -

def greeter(name)
return “Hello, #{name}!”
end

but

Why is # used in the second line, why not +?

Thanks!

I used a ‘+’ and it worked…

def greeter (name)
return “Hi” + “#{name}”
end

1 Like

That is known as string interpolation and meant to work in a single plain string expression. We would never use it in combination with concatenation.

 return "Hi " + name

OR

 return "Hi #{name}"
2 Likes

I need help with the second part of this lesson.
I’m not sure what I did wrong here:

def by_three?(number)
return by_three? == true
  if number % 3 == 0
  else
return by_three? == false
  end
end
output = by_three?(9)

I’ve tried everything possible and this code left me with only one error which I don’t understand:

“wrong number of arguments (given 0, expected 1)”

I did give an argument which was 9. So I’m pretty stuck.
Please help!

Are you new to programming? If the answer is yes, then, is there a reason why you chose Ruby as your first language? If the answer is because you need it for school or your job, then keep forging ahead, and buckle up. If the answer is because it looks like a cool language to learn, then switch to Python and give this language a pass.

Ruby is a cool language. Even school kids can and do learn it, especially in Finland. It’s a very sophisticated high level language that has tremendous computing power once we learn how to implement it. The learning curve is steep at first so learning it on one’s own rather than in a learning environment will only add to the burden and probably see the majority of learners run out of steam. Only learn this language if you really intend to stay with it and advance to Ruby on Rails and other scientific or data science pursuits. Cool as it is, it is not for everyone. Something to consider, now.

Hopefully you are still in the mood to explore this question.

We’re not sure that programming is anything but new to you because of the way return is used in your code. A complete novice would not see the recursion, so we won’t say anything more about that.

Ruby is really cool, once we get over the basic programming hurdles.

def by_three?(number)
  number % 3 == 0
end

puts by_three?(9)    #  true

The above is leveraged upon two things: 1) any comparison expression yields a boolean; and, 2) Ruby has implicit return of the last line in the block. We don’t see it, but there is a return keyword implied at the beginning of the code line in that method.

A newbie won’t see this very clearly, so forgive me for just blurting it out.

def by_three?(number)
  if number % 3 == 0
    return true
  else
    return false
  end
end

This is the literal interpretation of the earlier example, and one which a learner will come up with, given some basic lesson guidance. It is just as valid, but we can see that the more we learn, the more we reach for the logical approach that Ruby affords us. It doesn’t come early, and it doesn’t come easy for many learners. Ruby requires commitment and staying power to really uncover its chi. For tinkering, switch to Python and return to Ruby when your skills are sufficient enough in that language to understand the documentation and usage of this one.

11 Likes

Thanks for the help! Looking at it now, I seemed to have overthought it.

1 Like

Hi people !

Is someone could you please explain to me what & do ?

I tried to look for on the net but I can’t find any ressources…

Thanks for your help, I can’t figure it out by myself !

The single ampersand (&) is the Bitwise operator for AND.

1 Like

Hi mtf ! Thanks for your prompt answer :slight_smile:

Unfortunately I missed my question, I wanted to ask about % !

Example : if number % 3 == 0

Thank you !

Okay, that makes more sense, now. The, % is the modulo operator. Think modulation.

Given any integer count, we can divide it up into rows of length modulus plus the short row, modulo.

The modulo is the remainder of that division.

27 % 4

 1  2  3  4    #  row length, 4
 5  6  7  8
 9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
25 26 27       #  remainder length, 3

Of course we can see there are 7 rows.

27 / 4  =>  6

plust the remainder row.

27 % 4  =>  3

Further study

A calendar is a good example of an integer modulation. The week always ends on the day when x % 7 is zero.

(1..7).each do |x|
  puts "#{x} => #{x % 7}"
end
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
7 => 0

Given that fixed modulus (7) and the fixed names we give to the days of the week, we can relate to dates. We can explore this further if you would like to.

days = [
  'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
]

Our weeks in Canada end on Saturday. This list can be shifted to match your country.

Thank you so much for your explanation, it’s super clear now and your example with the calendar helps to understant how the % could be use for !

I’m trying to improve this “calendar” and if I’m stucked, I’ll ask for your help :slight_smile:

1 Like

I wasn’t able to figure out how to link the mudolo result with the array… By an hash ?

Here my code at this moment :

(1..7).each do |x|
  puts "#{x} => #{x % 7}"
end

days = ["saturday", "sunday", "monday", "tuesday", "wednesday", "thursday", "friday"]

days.each do |day|
  if day != "saturday"
    puts "#{day} is not the end of the week !"
  else
    puts "this is #{day} : end of the week !"
  end
end

Thanks for your help !

It’s not really a hash, per se, but I made it up to look similar since they are associated data points and descriptors. We could create a look-up hash and not even use modulo, afterward. Below is not that, but think of how it could be reversed to fulfill that promise.

days = [
  'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
]
day = Hash.new()
(1..7).each do |x|
  day[x % 7] = x
end
day.each {|k, v| puts "#{k} => #{v}"}
puts days[day[0] % 7]
1 => 1
2 => 2
3 => 3
4 => 4
5 => 5
6 => 6
0 => 7
Saturday

Hi mtf !

Thank your for your example :slight_smile:

I’m still a big big beginner so it helps a lot ! I was wondering how we could do to link each number with its day (for example 1 = monday and and 0 = saturday) and puts out all the days like that ! I’m not sure if you understand what I want to do lol, my english is not so fluent…

Any ideas ?

If 0 is Saturday, then 1 is Sunday. We cannot change the order of the days, that is fixed. The only variable is regional difference in terms of which day is considered the end of the week. It’s usually one or the other of Saturday or Sunday.

End of week => Saturday

days = [
  'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'
]

End of week => Sunday

days = [
  'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'
]

When Saturday is 0, Monday is 2. When Sunday is 0, Monday is 1. In the sequences above, the index is the modulo the day is paired up with.

day = Hash.new()
(1..7).each do |x|
  day[x] = x % 7
end
day.each {|k, v| puts "#{k} => #{v}"}
puts days[day[7]]

Pay particular attention to the above. It is a hash with keys 1 thru 7. Their values are the indices of the sequence.

1 Like

Ok I finally got it ! Pretty clear now :wink:

Thank you so much !

1 Like

Sincerity and straight talk is the best thing for someone who is getting started to don’t create fake illusions and get burnt from the very beginning

1 Like

def by_three?(number)

if number % 3 == 0

return true

else

return false

end

end

loop {

puts "Give me a number to check if it can be div by 3: "

num = gets.chomp

break if num.is_i? == true

}

I wonder why this gives me an “execution expired” message when I enter a random string in the console, to check if it actually re-prompts the user for an integer instead of a random input? Is the codeacademy environment limiting in any way when it comes to playing around with code?
Thank you in advance.