Shellwords

537 views

Published on

Method of the Month (MOTM) for the Columbus Ruby Brigade (CRB, ColumbusRB).

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
537
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Notice that the extra whitespace between seven and eight is (unsurprisingly) not preserved.\n
  • Notice that the extra whitespace between seven and eight is (unsurprisingly) not preserved.\n
  • Notice that the extra whitespace between seven and eight is (unsurprisingly) not preserved.\n
  • Notice that the extra whitespace between seven and eight is (unsurprisingly) not preserved.\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Shellwords

    1. 1. SHELLWORDSRuby Method/Module of the Month Kevin Munc - @muncman
    2. 2. WHAT IS IT?“This module manipulates strings according to the wordparsing rules of the UNIX Bourne shell.” (http://www.ruby-doc.org/ruby-1.9/classes/Shellwords.html)Conforms to POSIX / SUSv3 (IEEE Std 1003.1-2001)Built on String#scanrequire ‘shellwords’
    3. 3. SHELLWORDSmyString = "one-two three_four fiveSix seven eight nineten"
    4. 4. SHELLWORDSmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellwords(myString)
    5. 5. SHELLWORDSmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellwords(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"]
    6. 6. SHELLSPLITmyString = "one-two three_four fiveSix seven eight nineten"
    7. 7. SHELLSPLITmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellsplit(myString)
    8. 8. SHELLSPLITmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellsplit(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"] # same as shellwords
    9. 9. SHELLSPLITmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellsplit(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"] # same as shellwordsShellwords.split(myString) # alias
    10. 10. SHELLESCAPEmyString = "one-two three_four fiveSix seven eight nineten"
    11. 11. SHELLESCAPEmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellescape(myString)
    12. 12. SHELLESCAPEmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellescape(myString)==> "one-two three_four fiveSix seven eight nine ten"
    13. 13. SHELLESCAPEmyString = "one-two three_four fiveSix seven eight nineten"Shellwords.shellescape(myString)==> "one-two three_four fiveSix seven eight nine ten"Shellwords.escape(myString) # alias
    14. 14. SHELLJOINmyArray = Shellwords.shellwords(myString)
    15. 15. SHELLJOINmyArray = Shellwords.shellwords(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"]
    16. 16. SHELLJOINmyArray = Shellwords.shellwords(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"]Shellwords.shelljoin(myArray)
    17. 17. SHELLJOINmyArray = Shellwords.shellwords(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"]Shellwords.shelljoin(myArray)==> "one-two three_four fiveSix seven eight nine ten"
    18. 18. SHELLJOINmyArray = Shellwords.shellwords(myString)==> ["one-two", "three_four", "fiveSix", "seven", "eight", "nineten"]Shellwords.shelljoin(myArray)==> "one-two three_four fiveSix seven eight nine ten"Shellwords.join(myArray) # alias
    19. 19. STRING & ARRAYSHORTHAND
    20. 20. STRING & ARRAYSHORTHANDmyString.shellsplit
    21. 21. STRING & ARRAYSHORTHANDmyString.shellsplitmyString.shellescape
    22. 22. STRING & ARRAYSHORTHANDmyString.shellsplitmyString.shellescapemyArray.shelljoin
    23. 23. STRING & ARRAYSHORTHANDmyString.shellsplitmyString.shellescapemyArray.shelljoinmyString.shellwords # nope
    24. 24. STRING & ARRAYSHORTHANDmyString.shellsplitmyString.shellescapemyArray.shelljoinmyString.shellwords # nope# no shorthand method names, either
    25. 25. QUESTIONS? Photo Credits: http://www.flickr.com/photos/davidbygott/5241519842/ http://www.flickr.com/photos/34878947@N04/4450370442/

    ×