Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Making things the hard way
#2
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.

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.
Up is down, left is right and sideways is straight ahead. - Cord "Circle of Iron", 1978 (written by Bruce Lee and James Coburn... really...)
[Image: QrnbKlx.jpg]
[Image: sGz1ErF.png] [Image: liM4ikn.png] [Image: fdzKgZA.png] [Image: sj0H81z.png]
[Image: QL7oRau.png] [Image: uSqjY09.png] [Image: GAA3qE9.png] [Image: 2Hmnx1G.png] [Image: BwtNdKw.png%5B]
Above are clickable links

Reply }


Messages In This Thread
Making things the hard way - by kyonides - 09-24-2018, 02:28 AM
RE: Making things the hard way - by DerVVulfman - 09-24-2018, 04:31 PM
RE: Making things the hard way - by MetalRenard - 09-25-2018, 12:17 AM
RE: Making things the hard way - by DerVVulfman - 09-25-2018, 03:24 AM
RE: Making things the hard way - by kyonides - 10-01-2018, 07:59 AM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 03:10 AM
RE: Making things the hard way - by kyonides - 10-02-2018, 01:24 PM
RE: Making things the hard way - by DerVVulfman - 10-02-2018, 11:59 PM
RE: Making things the hard way - by kyonides - 10-03-2018, 12:55 AM
RE: Making things the hard way - by DerVVulfman - 10-03-2018, 03:36 AM
RE: Making things the hard way - by kyonides - 12-10-2021, 08:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
   Making a "Wait Until Button Input" PK8 1 5,775 05-28-2012, 12:50 PM
Last Post: yamina-chan
   Making a Successful comedy RPG DerVVulfman 0 4,920 05-17-2011, 05:18 AM
Last Post: DerVVulfman
   The Ultimate Guide for Making Nifty Fantasy RPGs by Magus Masque DerVVulfman 5 10,469 06-07-2010, 02:44 PM
Last Post: sakhawat21
   Making an Impact, Part 3 Kaos Tenshi 0 3,554 01-19-2009, 05:33 PM
Last Post: Kaos Tenshi
   Making an Impact, Part 2 Kaos Tenshi 0 3,507 01-19-2009, 05:30 PM
Last Post: Kaos Tenshi
   Making An Impact, Part 1 Kaos Tenshi 0 3,593 01-19-2009, 05:26 PM
Last Post: Kaos Tenshi
   Rose Skye's Game Making Tips RoseSkye 0 3,397 12-06-2008, 05:06 PM
Last Post: RoseSkye



Users browsing this thread: