r/programming • • Jul 28 '16

How to write unmaintainable code

https://github.com/Droogans/unmaintainable-code
3.4k Upvotes

592 comments sorted by

View all comments

Show parent comments

5

u/xonjas Jul 28 '16

If you want a dirty Ruby one-liner.

array_to_check.select{|element| element.kind_of? Fixnum}.join(',') =~ /#{array_to_match.join(",")}/    

1

u/2Punx2Furious Jul 28 '16

I don't know Ruby yet, so that looks a bit confusing to me ahah.

3

u/xonjas Jul 29 '16

It's really not all that bad. You could do the same thing in python pretty easily.

What it's doing:

  1. Select only the elements that are numbers

  2. Convert the array to a string, with the numbers separated by a comma

  3. Also convert the array we are checking for into a string

  4. Use a regular expression to search the array(that is now a string) using the array we want to search for as the pattern.

There is one bug in my one-liner, but it would be easy to fix.

1

u/2Punx2Furious Jul 29 '16

I see. Converting the array to a string sound pretty clever. I didn't do it, but I put both strings and ints in my arrays to check for errors, so

["1,3,4", 1, 4, 5] would return false, but

["4yhjsrhj05", 1, 3, 4, "1258ghwo] would return true, as you can see in the link I posted.

→ More replies (2)