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!
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
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.
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
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