FAQ: Object-Oriented Programming II - Extend Your Knowledge

This community-built FAQ covers the “Extend Your Knowledge” 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 Extend Your Knowledge

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!

What part of the code decides if the time is GMT versus PST which is where I am?

Because CC is world-wide, it uses GMT (or more commonly known today as UTC) as its timezone of reference. If you run this code on your machine in a local environment, it will refer to your system time, which represents the timezone you are in.

Not accounting for Daylight Savings Time, your zone is offset from UTC by -8 hours. If DST applies in your region, it is currently offset by -7 hours.

That means you can add the offset in your CC code and it will reflect the current time in your area.

module ThePresent
  def now
    offset = -8 # -7 if PDT in effect
    present = Time.now
    hour = present.hour + offset
    puts "It's #{hour > 12 ? hour - 12 : hour}:#{Time.new.min} #{hour > 12 ? 'PM' : 'AM'} (UTC#{offset})."
  end
end
1 Like

Can anybody walk me through this code ? I don’t understand how it works

def now
puts “It’s #{Time.new.hour > 12 ? Time.new.hour - 12 : Time.new.hour}:#{Time.new.min} #{Time.new.hour > 12 ? ‘PM’ : ‘AM’} (GMT).”
end

Ok actually I think I got my head around this. I’ll try out a little explanation to verify my accuracy. So basically that first “?” tells ruby to evaluate if the current time is greater than 12. if it is, then its subtracts 12. If not, then it outputs the current time. Next we get the minutes. Once again ruby evaluates if the current time is greater than 12. If so it outputs PM, and, if not, it outputs AM.

? is known as the ternary operator. There are three parts to a ternary expression.

condition/state ? action/value if true/truthy : default action/value

String interpolation permits any expression,

"#{any_expression}"

This permits the ternary which outputs one of the two values depending upon the outcome of the conditional. Your assessment is correct.

If the statement is written in the learning environment of CC, it will in fact be GMT (Greenwich Mean Time). On our own system it will be GMT +/- timezone offset based on where we live. Plus if we live east of the Prime Meridian, and minus if we live west of it. The International Date Line separates the two regions of the world.

In modern terms we use UTC instead of GMT, but the principal is the same.

 MST == UTC -7:00h

so when it is lunch time in London, it is 5:00 AM in the Prairies.

1 Like

Not sure to really grasp the concept because in this example if I code ‘include ThePresent’ instead of ‘extend ThePresent’, ‘TheHereAnd.now’ still works. Can someone enlighten me?

Be sure to do a hard refresh before testing changes. There may be things in memory that trick us into believing something works, when it actually does not.

class TheHereAnd
    The Present
end
undefined method `now' for Context::TheHereAnd:Class
Did you mean?  new

Effectively, thanks mtf!

1 Like

So here I understand that while we’ve not defined any method in TheHereAnd, it’s taking the now method we created in ThePresent to give the time.

However if I add ThePresent.now

I get an error message: undefined method now’ for Context::ThePresent:Module` ?
Any reason why it’s not giving me the time twice?
one for TheHereAnd.now
and one for ThePresent.now