FAQ: Object-Oriented Programming II - The Marriage of Modules and Classes

This community-built FAQ covers the “The Marriage of Modules and Classes” exercise from the lesson “Object-Oriented Programming II”.

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

Learn Ruby

FAQs on the exercise The Marriage of Modules and Classes

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 smb please explain why the outputs for rabbit and cricket are different? We didn’t pass any arguments to alter behavior… Thanks!

1 Like

Hey! This is from the rand() in the def action method

def jump
    @distance = rand(4) + 2
    puts "I jumped forward #{@distance} feet!"

The 4 in rand(4) will make the random number generated be between 0-4. If you rerun the program sometimes the results are the same and sometimes they are different.

This might be helpful for more info: https://blog.appsignal.com/2018/07/31/generating-random-numbers-in-ruby.html


I understand why that text is out put, but the bit I don’t get is why any text is output at all as we didn’t call ‘jump’ as best I can tell.

EDIT: Ignore me, needed to scroll down to see the last two lines where jump was called.

Can someone please explain me why attr_reader is needed?

Every class worked just fine without them before. I feel these methods were introduced and explained poorly and now they seem to be mandatory in every class… Why do we need to read something before initializing it? Why do we need to read it in any case actually?

1 Like

I have the same question. I thought they replaced the methods, but in this particular example the methods are still defined. Hopefully someone can elaborate.

What I understand is that attr_reader is creating a “getter” method.

It says that now you can access the object name and read it with a given parameter in the block you are calling (“Peter” / “Jiminy”) outside the block.

When you use the attr_reader method in your class, it works like a shortcut that allows this method to be accessed outside the class. Not sure if this is right, but I hope so :slight_smile: