Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Fetch the lab 1 working string into a variable named the_string
- require "open-uri" # Use this library to do network requests
- the_string = "" # Initialize this string
- the_url = "http://hills.ccsf.edu/~dputnam/the_string.cgi"
- open(the_url) do |content|
- content.each_line { |line| the_string += line }
- end
- puts "6.1 - Beginning length analyis of the_string.\n\n"
- print "The length of the_string is: ", the_string.length, "\n\n"
- puts "6.2 - Removing whitespace characters from the_string.\n\n"
- string_clean = the_string.squeeze(" ")
- string_clean = string_clean.squeeze("\t")
- print string_clean
- puts "\n 6.3 - Downcase the_string.\n\n"
- puts the_string.downcase
- puts "\n 6.4 - Upcase the_string.\n\n"
- puts the_string.upcase
- puts "\n6.5 - Remove X's from the end of the line.\n\n"
- ## Removing X's only followed by regex end of string Character $.
- puts the_string.gsub(/X$/, '')
- puts "\n6.6 - Printing the string per character, decimal, and hex values.\n\n"
- the_string.scan(/./) {|char|
- print char, ':'
- print char.ord, ','
- print char.ord, ','
- print "%02Xs" % char.ord, ' '
- print "\n"
- }
- puts "\n\n6.7 - Splitting the_line into an array of words\n\n"
- puts the_string.split
- puts "\n6.8 - Creating a crypt hash using a standard salt string.\n\n"
- puts the_string.crypt('AaA00')
- puts "\n6.9 - Capitalizing the first letter in each line.\n\n"
- the_string_array = the_string.split(/\n/)
- the_string_array.each {|line|
- # I wonder if there is a way to combine these two variables
- find_lower = line.match(/\W([a-z])/)
- find_lower = find_lower[0].to_str
- upper = find_lower.upcase
- # Regexp.quote is needed because find_lower was triggering a regex
- # on "(b" from the line.
- puts line.to_str.sub(/#{Regexp.quote(find_lower)}/, "#{upper}")
- }
- puts "\n6.10 - Running strip, squeeze, reverse, and upcase on the same object!\n\n"
- puts the_string.object_id
- puts
- the_string[0...100] = the_string[0...100].strip! if !the_string[0...100].strip!.nil?
- the_string[0...100] = the_string[0...100].squeeze!(" ") if !the_string[0...100].squeeze!(" ").nil?
- the_string[0...100] = the_string[0...100].squeeze!("\t") if !the_string[0...100].squeeze!("\t").nil?
- the_string[0...100] = the_string[0...100].reverse! if !the_string[0...100].reverse!.nil?
- the_string[0...100] = the_string[0...100].upcase! if !the_string[0...100].upcase!.nil?
- puts the_string
- puts
- puts the_string.object_id
- puts "\n6.11 - Printing kernel\#inspect method!\n\n"
- puts the_string.inspect
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement