Skip to content

Instantly share code, notes, and snippets.

@danjenson
Created August 25, 2014 16:41
Show Gist options
  • Save danjenson/b171271726198630e245 to your computer and use it in GitHub Desktop.
Save danjenson/b171271726198630e245 to your computer and use it in GitHub Desktop.
ruby -e "('A'..'Z').each { |x| ('A'..x).each { |y| print y == 'E' ? 'X' : y }; puts }"
@danjenson
Copy link
Author

  1. Outer loop 'A'..'Z' loops from A->Z sending a character to the variable 'x' in the block.
  2. 'x' is used as the end point of the inner range, which is iterated over, yielding a character from A->'x' to the block as 'y'.
  3. The block prints 'y', but checks first to see if it equals 'E' using a ternary operator (http://stackoverflow.com/questions/4252936/how-do-i-use-the-conditional-operator-in-ruby). If the character equals 'E', it switches it with 'X', otherwise it prints the original character, 'y'.
  4. After each inner loop, puts prints a new line (by default).

@Macariom01
Copy link

Show off!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment