Writing a long hash for a file converter [closed]

Posted on


I’ve created a hash for a file converter. The file gets loaded into the program, and from there the file goes to a hex editor and the user picks and chooses what they want to do to the file. During the users’ choices, the choices will run against a hash containing all hex information. It will then translate the hex and return it back to the file. The user will then use the another part of the program to resave the file with a new extension (still a work in progress).

I have a super long hash and was wondering if there’s a better way to write the hash. I won’t be posting the entire hash due to shear size of it.

hexadecimal_translate = { 
                        '00' => ["n", "u", "l"],
                        '01' => ["s", "o", "h"],
                        '02' => ["s", "t", "x"],                        
                        '03' => ["e", "t", "x"],
                        '04' => ["s", "e", "l"],
                        '05' => ["t", "a", "b"],
                        '06' => ["r", "n", "l"] ,
                        '07' => ["d", "e", "l"],
                        '08' => ["g", "e"], 
                        '09' => ["s", "p", "s"],
                        '10' => ["r", "p", "t"], 
                        '11' => vt,
                        '12' => ff,
                        '13' => cr,
                        '14' => so,
                        '15' => si, #<= All the way to 99 => r,
                        '0a' => rpt,
                        '0b' => vt,
                        '0c' => ff, 
                        '0d' => cr,
                        '0e' => so,
                        '0f' => si,
                        '1a' => ubs,
                        '1b' => cu1,
                        '1c' => ifs,
                        '1d' => igs, 
                        '1e' => irs, #This is by far the scariest number I've ever seen.
                        '1f' => itb # need to also make it able to support IUS
                        # all the way to 9f => which equals nothing btw


Ruby uses SCREAMING_SNAKE_CASE for constants, which is what you are setting up, so I would call the variable HEX_MAP. If you have a super long constant, and it’s not created programmatically, then listing it a line at a time is clearest.

Ruby will coerce 00 into 0 unless you treat it as a string. You can use the shorthand for arrays. So, the first couple lines should be:

'00' => %w(n u l),
'01' => %w(s o h),

Leave a Reply

Your email address will not be published. Required fields are marked *