Skip to content

Instantly share code, notes, and snippets.

@rplugge
Created June 8, 2015 19:29
Show Gist options
  • Save rplugge/7ba71210757e652208ac to your computer and use it in GitHub Desktop.
Save rplugge/7ba71210757e652208ac to your computer and use it in GitHub Desktop.
Mini-projects
class PhoneSplitter
attr_reader :number_array
def initialize(number)
@number = number
@number_array = []
end
# - Checks to see if the number provided is proper length.
def correct_length?
if @number.length == 10
true
else
false
end
end
# - Splits numbers and adds them to number_array
def number_array
@number_array = @number.split("")
end
# - Returns the number in (xxx)xxx-xxxx form
def return_number
return "(#{number_array[0]}#{number_array[1]}#{number_array[2]})#{number_array[3]}#{number_array[4]}#{number_array[5]}\
-#{number_array[6]}#{number_array[7]}#{number_array[8]}#{number_array[9]}#{number_array[10]}"
end
puts "Please enter your phone number as one whole number - e.g. 1234567890"
number = gets.chomp.to_s
new_number = PhoneSplitter.new(number)
if new_number.correct_length? == false
abort("Sorry, that is not a valid number")
end
new_number.number_array
puts "You're phone number (reformatted) is '#{new_number.return_number}'"
end
# - "Bike" is just random placeholder text.
class SlimParagraph
attr_accessor :bike
# - Set's 'bike' definition.
def initialize
@bike = "Something you ride on to get to places, or for exercise."
end
# - Returns portion of definition based on how many characters the user wants to see.
def first_words(x)
@bike[0...x] + "..."
end
def all_words
@bike
end
end
this_user = SlimParagraph.new
puts "Would you like to see this definition?"
answer = gets.chomp.downcase
if answer != "no"
puts "How many characters would you like to see? (E.g. 5, 15, 20)"
answer = gets.chomp.to_i
puts this_user.first_words(answer)
else
abort("Goodbye!")
end
puts "Would you like to see the whole definition?"
answer = gets.chomp.downcase
if answer != "no"
puts this_user.all_words
else
abort("Goodbye!")
end
# - Super basic, no class.
# - Returns all but the last object in the array, then adds 'and' followed by last object.
array = ["Red", "Blue", "Green", "Yellow", "Orange"]
x = array.length
y = x - 2
z = x -1
puts array[0..y].join(", ") + ", and #{array[z]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment