Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Percent Literals (MOTM 2010.09)

1,014 views

Published on

Ruby Method of the Month 2010.09. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Percent Literals (MOTM 2010.09)

  1. 1. % PERCENT LITERALS RUBY METHOD (SORT OF) OF THE MONTH KEVIN MUNC @MUNCMAN
  2. 2. WHAT DOES THIS DO? %%COLUMBUS RUBY BRIGADE%
  3. 3. WHAT DOES THIS DO? %%COLUMBUS RUBY BRIGADE% "COLUMBUS RUBY BRIGADE"
  4. 4. %Q AND %q ‘Q’ is for Quotes No escaping quotation marks in your strings! Uppercase Q acts like double-quotes. You get String interpolation. You can leave off ‘Q’, but it’s not recommended. Lowercase Q acts like single-quotes.
  5. 5. %Q AND %q Put a delimiter after the ‘Q’ and at the end. Choose any delimiter you want. Even semicolon (;), quotes (“ or ‘) or percent (%) itself. Letters can be problematic as delimiters. Just don’t use a character that is in the delimited value.
  6. 6. % Instead of: "This is "Spinal Tap"" You can do this: %q{This is "Spinal Tap"}
  7. 7. % Instead of: "This is "Spinal Tap"" You can do this: %q{This is "Spinal Tap"} Or instead of this: what = "Glove" "Smell the "#{what}"" You can do this: %Q{Smell the "#{what}"}
  8. 8. % Instead of: "This is "Spinal Tap"" You can do this: %q{This is "Spinal Tap"} Or instead of this: what = "Glove" "Smell the "#{what}"" You can do this: %Q{Smell the "#{what}"} Or instead of this: ""None" "more" "black"" You can do this: %q<"None" "more" "black">
  9. 9. %W AND %w ‘W’ is for Words (or Whitespace). Let you create an array of strings: Without needing quotes. Without needing commas.
  10. 10. %W AND %w Uppercase W allows for string interpolation. Lowercase W does not. You cannot use quotes to group together a phrase containing whitespace. Instead, escape the whitespace.
  11. 11. % %w!Break Like the Wind!
  12. 12. % %w!Break Like the Wind! ["Break", "Like", "the", "Wind"]
  13. 13. % %w!Break Like the Wind! ["Break", "Like", "the", "Wind"] %W|Hello Cleveland! #{what}|
  14. 14. % %w!Break Like the Wind! ["Break", "Like", "the", "Wind"] %W|Hello Cleveland! #{what}| ["Hello", "Cleveland!", "Glove"]
  15. 15. % %w[Shark Sandwich "Big Bottom"]
  16. 16. % %w[Shark Sandwich "Big Bottom"] ["Shark", "Sandwich", ""Big", "Bottom""]
  17. 17. % %w[Shark Sandwich "Big Bottom"] ["Shark", "Sandwich", ""Big", "Bottom""] %w[Shark Sandwich Big Bottom]
  18. 18. % %w[Shark Sandwich "Big Bottom"] ["Shark", "Sandwich", ""Big", "Bottom""] %w[Shark Sandwich Big Bottom] ["Shark", "Sandwich", "Big Bottom"]
  19. 19. %
  20. 20. %x ‘X’ is for eXecute. Let’s you specify a system command without the backtick, backquote or grave accent marks (`). But you can use backticks as delimiters if you’d like - if they aren’t in the command... There is no uppercase version. String interpolation is supported.
  21. 21. % %x(date)
  22. 22. % %x(date) "Mon Sep 20 01:10:55 EDT 2010n"
  23. 23. % %x(date) "Mon Sep 20 01:10:55 EDT 2010n" %x$pwd$
  24. 24. % %x(date) "Mon Sep 20 01:10:55 EDT 2010n" %x$pwd$ "/Users/muncmann"
  25. 25. %r ‘R’ is for Regular Expression. Let’s you specify a regex without having to escape forward slashes (/). But you can use slashes as delimiters if you’d like - if they aren’t in the regex... There is no uppercase version. String interpolation is supported.
  26. 26. Instead of: % You can do this: //usr/local/bin/ %r{/usr/local/}
  27. 27. %s ‘S’ is for Symbol. This seems to be the least known percent literal of the bunch. There is no uppercase version. String interpolation is not supported.
  28. 28. % %s{stonehenge}
  29. 29. % %s{stonehenge} :stonehenge
  30. 30. % %s{stonehenge} :stonehenge %s{Tonight I'm Gonna Rock You Tonight}
  31. 31. % %s{stonehenge} :stonehenge %s{Tonight I'm Gonna Rock You Tonight} :"Tonight I'm Gonna Rock You Tonight"
  32. 32. QUESTIONS?

×