09-24-2018, 04:31 PM
(This post was last modified: 09-24-2018, 04:40 PM by DerVVulfman.)
There's doing things the hard way, and doing things the error-free way.
While you may feel the ternary condition would be easier, it can generate errors if incorrect data was passed into the method. As such, a command of "int_month('Fred')" would crash the system as your system as you would generate a data comparison error that wasn't accounted for.
However, your earlier case...end method is safer. Using the script call "int_month('Fred')" would return the 'Invalid' month message of "Current Month is Fred or better known as invalid." The case method doesn't necessarily require data to be of one format or another, so it can return the invalid response if incorrect data was passed.
However, you could add a mere single line to fix this issue, one that ensures the data passed is an integer. In the example below, I added the statement of "month_int = month.to_i" which takes your passed data and converts it before pushing it into 'month_int' ... or month-as-integer.
Of course, I also had to change the later 'month_name = ...' statement to accommodate the new value. However, the new statement prevents the method from breaking.
Error detection and prevention is certainly top priority. There can always be different ways to write a piece of code, and 'truthfullly", the ternary method can always expand what data can be passed if the array of months (in this example) had more months pushed into it. The case...end method is more rigid and wouldn't let you test any more data than what is coded in comparison. However, the ternary method in your example does requires that the data being tested must be integer in nature.
EDIT: For those unaware, 'puts' is an actual statement in the IO (input/output) class for file processing. You could use 'p' if you just want it shown on the screen.
While you may feel the ternary condition would be easier, it can generate errors if incorrect data was passed into the method. As such, a command of "int_month('Fred')" would crash the system as your system as you would generate a data comparison error that wasn't accounted for.
However, your earlier case...end method is safer. Using the script call "int_month('Fred')" would return the 'Invalid' month message of "Current Month is Fred or better known as invalid." The case method doesn't necessarily require data to be of one format or another, so it can return the invalid response if incorrect data was passed.
However, you could add a mere single line to fix this issue, one that ensures the data passed is an integer. In the example below, I added the statement of "month_int = month.to_i" which takes your passed data and converts it before pushing it into 'month_int' ... or month-as-integer.
Code:
def int_month(month)
month_int = month.to_i
months = ["invalid",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"]
month_name = month_int.between?(1, 12)? months[month] : months[0]
puts "Current Month is #{month} or better known as #{month_name}."
end
Of course, I also had to change the later 'month_name = ...' statement to accommodate the new value. However, the new statement prevents the method from breaking.
Error detection and prevention is certainly top priority. There can always be different ways to write a piece of code, and 'truthfullly", the ternary method can always expand what data can be passed if the array of months (in this example) had more months pushed into it. The case...end method is more rigid and wouldn't let you test any more data than what is coded in comparison. However, the ternary method in your example does requires that the data being tested must be integer in nature.
EDIT: For those unaware, 'puts' is an actual statement in the IO (input/output) class for file processing. You could use 'p' if you just want it shown on the screen.