Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # conversions… computer doesn't know if we want to add 2 + 5 = 7 or 2 and 5 = 25, so need to convert var1 to a string in order to put 25
- var1 = 2
- var2 = '5'
- puts var1.to_s + var2
- # and vice-versa to put 7 via conerting a string to an integer
- puts var1 + var2.to_i
- # other conversions - note: computer ignores first thing it doesn't understand. rb.14/15
- puts '15'.to_f
- puts '99.999'.to_f
- puts '99.999'.to_i
- puts ''
- puts '5 is my favorite number!'.to_i
- puts 'Who asked you about 5 or whatever?'.to_i
- puts 'Your momma did.'.to_f
- puts ''
- puts 'stringy'.to_s
- puts 3.to_i
- # Before puts tries to write out an object, it uses to_s to get the string version of that object. In fact, the s in puts stands for string; puts really means put string.
- puts 20
- puts 20.to_s
- puts '20'
- # gets and chomp methods
- puts 'Hello there, and what\'s your name?'
- name = gets.chomp
- puts 'Your name is ' + name + '? What a lovely name!'
- puts 'Pleased to meet you, ' + name + '. :)'
- # asks for a person's first name, then middle, then last. Finally, it should greet the person using their full name
- puts 'Hello! What\'s your first name?'
- firstName = gets.chomp
- puts 'Great! ' + firstName + ', what\'s your middle name?'
- middleName = gets.chomp
- puts 'OK, ' + firstName + ' ' + middleName + ', what\'s your last name?'
- lastName = gets.chomp
- puts 'Wonderful! Welcome to my domain, ' + firstName + ' ' + middleName + ' ' + lastName + '!'
- # Write a program which asks for a person's favorite number. Have your program add one to the number, then suggest the result as a bigger and better favorite number.
- puts 'Yo dude, what\'s your favorite number?'
- number = gets.chomp
- new_number = number.to_i + 1
- puts 'OK. That\'s nice and all, but I reckon ' + new_number.to_s + ' is bigger and better.'
Add Comment
Please, Sign In to add comment