Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.48 KB | None | 0 0
  1. # Fetch the lab 1 working string into a variable named the_string
  2. require "open-uri" # Use this library to do network requests
  3. the_string = ""    # Initialize this string
  4. the_url = "http://hills.ccsf.edu/~dputnam/the_string.cgi"
  5.  
  6. open(the_url) do |content|
  7.   content.each_line { |line| the_string += line }
  8. end
  9.  
  10. puts "6.1 - Beginning length analyis of the_string.\n\n"
  11. print "The length of the_string is: ", the_string.length, "\n\n"
  12.  
  13. puts "6.2 - Removing whitespace characters from the_string.\n\n"
  14. string_clean = the_string.squeeze(" ")
  15. string_clean = string_clean.squeeze("\t")
  16. print string_clean
  17.  
  18. puts "\n 6.3 - Downcase the_string.\n\n"
  19. puts the_string.downcase
  20.  
  21. puts "\n 6.4 - Upcase the_string.\n\n"
  22. puts the_string.upcase
  23.  
  24. puts "\n6.5 - Remove X's from the end of the line.\n\n"
  25. ## Removing X's only followed by regex end of string Character $.
  26. puts the_string.gsub(/X$/, '')
  27.  
  28. puts "\n6.6 - Printing the string per character, decimal, and hex values.\n\n"
  29.  
  30. the_string.scan(/./) {|char|
  31.   print char, ':'
  32.   print char.ord, ','
  33.   print char.ord, ','
  34.   print "%02Xs" % char.ord, ' '
  35.   print "\n"
  36. }
  37.  
  38. puts "\n\n6.7 - Splitting the_line into an array of words\n\n"
  39. puts the_string.split
  40.  
  41. puts "\n6.8 - Creating a crypt hash using a standard salt string.\n\n"
  42. puts the_string.crypt('AaA00')
  43.  
  44. puts "\n6.9 - Capitalizing the first letter in each line.\n\n"
  45.  
  46. the_string_array = the_string.split(/\n/)
  47.  
  48. the_string_array.each {|line|
  49.   # I wonder if there is a way to combine these two variables
  50.   find_lower = line.match(/\W([a-z])/)
  51.   find_lower = find_lower[0].to_str
  52.   upper = find_lower.upcase
  53.   # Regexp.quote is needed because find_lower was triggering a regex
  54.   # on "(b" from the line.
  55.   puts line.to_str.sub(/#{Regexp.quote(find_lower)}/, "#{upper}")
  56. }
  57.  
  58. puts "\n6.10 - Running strip, squeeze, reverse, and upcase on the same object!\n\n"
  59. puts the_string.object_id
  60. puts
  61.  
  62. the_string[0...100] = the_string[0...100].strip! if !the_string[0...100].strip!.nil?
  63. the_string[0...100] = the_string[0...100].squeeze!(" ") if !the_string[0...100].squeeze!(" ").nil?
  64. the_string[0...100] = the_string[0...100].squeeze!("\t") if !the_string[0...100].squeeze!("\t").nil?
  65. the_string[0...100] = the_string[0...100].reverse! if !the_string[0...100].reverse!.nil?
  66. the_string[0...100] = the_string[0...100].upcase! if !the_string[0...100].upcase!.nil?
  67.  
  68. puts the_string
  69.  
  70. puts
  71. puts the_string.object_id
  72.  
  73.  
  74. puts "\n6.11 - Printing kernel\#inspect method!\n\n"
  75. puts the_string.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement