Search code examples
rubyregexarraysstringblock

Unused regex captures in Ruby


I have a script that processes the contents of a file from a CAD program, for use in another CAD program. Can the unused variables in the block be skipped, or written around? The script works fine with them in place, I was just curious if there was a cleaner way to write it. Thank you.

    string = IO.read("file.txt")

    string.scan(/regex/m) {|a,b,c,d,e,f,g|

    # captures 7 items, I use 1-4, & 6 below, skipping 5 & 7

    print a, b+".ext", c.to_f/25400000, d.to_f/25400000, f,"\n"
    }

My question lies in the last line - if I'm not using them all - do I still have to declare them all, for it to work properly, and remain in the correct order?

Elements 5 & 7 may be used at a later time, but for now, they are just part of the regex, for future flexibility.


Solution

  • You could use an array instead of an explicit list of variables and then pick things out of the array by index:

    string.scan(/regex/m) { |a|
        print a[0], a[1] + ".ext", a[2].to_f / 25400000, a[3].to_f / 25400000, a[5], "\n"
    }
    

    Either that or rework your regular expression to only capture what you need.

    You can use the same variable multiple times in the list so just renaming the things you're not using to unused would probably be the simplest choice:

    string.scan(/regex/m) { |a, b, c, d, unused, f, unused|
        print a, b + ".ext", c.to_f / 25400000, d.to_f / 25400000, f, "\n"
    }
    

    At least this way it is (or should be) obvious that you're not using the fifth and seventh captures. However, this doesn't work in 1.9 so you'd have to use unused1 and unused2 in 1.9.

    An ideal balance would be to use 1.9's named capture groups but scan doesn't give you access to them.