Created
June 10, 2015 18:01
-
-
Save KBeltz/61f195f2623bd9322806 to your computer and use it in GitHub Desktop.
Active Support Mini Programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support" | |
require "active_support/core_ext/string/filters.rb" | |
# Truncates a given +text+ after a given <tt>length</tt> if +text+ is longer than <tt>length</tt>: | |
# | |
# Example | |
# | |
# 'And they found that many people were sleeping better.'.truncate(25, omission: '... (continued)') | |
# # => "And they f... (continued)" | |
puts "<inspirational paragraph prompt here>:" | |
words = gets.chomp | |
puts "How many characters should be in that lovely paragraph of yours?" | |
number = gets.to_i | |
puts "What would you like to see at the end of your truncated paragraph?" | |
puts "(Ex: Read More, See More, ..., etc.)" | |
end_of_paragraph = gets.chomp | |
puts words.truncate(number, omission: end_of_paragraph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support" | |
require "active_support/core_ext/numeric/conversions.rb" | |
# Converts numbers into formatted strings. | |
# | |
# Phone Numbers: | |
# 5551234.to_s(:phone) # => 555-1234 | |
# 1235551234.to_s(:phone) # => 123-555-1234 | |
# 1235551234.to_s(:phone, area_code: true) # => (123) 555-1234 | |
# 1235551234.to_s(:phone, delimiter: ' ') # => 123 555 1234 | |
# 1235551234.to_s(:phone, area_code: true, extension: 555) # => (123) 555-1234 x 555 | |
# 1235551234.to_s(:phone, country_code: 1) # => +1-123-555-1234 | |
# 1235551234.to_s(:phone, country_code: 1, extension: 1343, delimiter: '.') | |
# # => +1.123.555.1234 x 1343 | |
# | |
# gets input from user | |
puts "Please enter a phone number: " | |
phone_number = gets.to_i | |
# active_support will reformat 1112223333 to 111-222-3333 | |
puts phone_number.to_s(:phone) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "active_support" | |
require "active_support/core_ext/array/conversions.rb" | |
# Converts the array to a comma-separated sentence where the last element is | |
# joined by the connector word. | |
# | |
# [].to_sentence # => "" | |
# ['one'].to_sentence # => "one" | |
# ['one', 'two'].to_sentence # => "one and two" | |
# ['one', 'two', 'three'].to_sentence # => "one, two, and three" | |
# | |
# ['one', 'two'].to_sentence(passing: 'invalid option') | |
# # => ArgumentError: Unknown key :passing | |
# | |
# ['one', 'two'].to_sentence(two_words_connector: '-') | |
# # => "one-two" | |
# | |
# ['one', 'two', 'three'].to_sentence(words_connector: ' or ', last_word_connector: ' or at least ') | |
# # => "one or two or at least three" | |
puts "Please list your favorite color or colors, without using commas:" | |
favorite_colors = gets.chomp | |
# allows only valid input | |
while favorite_colors.include?(",") | |
puts "Invalid. Please try again without using commas:" | |
favorite_colors = gets.chomp | |
end | |
# splits string at each " " into an array | |
favorite_colors = favorite_colors.split | |
if favorite_colors.length == 1 | |
puts favorite_colors.to_sentence | |
elsif favorite_colors.length == 2 | |
puts favorite_colors.to_sentence | |
else | |
puts favorite_colors.to_sentence(words_connector: ' , ', last_word_connector: ', and ') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment