Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 5 + 5 is really just a shortcut way of writing 5.+ 5
- puts 'hello '. + 'world'
- puts (10.* 9).+ 9
- # if I say puts 'to be or not to be', what I am really saying is self.puts 'to be or not to be'. So what is self? It's a special variable which points to whatever object you are in. (ex. "Die!" -> implicit noun is whoever you are saying it to. My computer tells me when don't need the 'self'
- # iCantBelieveIMadeAVariableNameThisLongJustToPointToA3 = 3
- # puts iCantBelieveIMadeAVariableNameThisLongJustToPointToA3
- # self.puts iCantBelieveIMadeAVariableNameThisLongJustToPointToA3
- # fancy string methods (reference method documents for ruby, just like a dictionary is for words)
- # reverse
- var1 = 'stop'
- var2 = 'stressed'
- var3 = 'Can you pronounce this sentence backwards?'
- puts var1.reverse
- puts var2.reverse
- puts var3.reverse
- puts var1
- puts var2
- puts var3
- #length
- puts 'What is your full name?'
- name = gets.chomp
- puts 'Did you know there are ' + name.length.to_s + ' characters in your name, ' + name + '?'
- # write a program which asks for your first, middle, and last names individually, and then adds those lengths together
- puts 'What is your first name?'
- firstName = gets.chomp
- puts 'What is your middle name?'
- middleName = gets.chomp
- puts 'What is your last name?'
- lastName = gets.chomp
- fullName = firstName.length.to_i + middleName.length.to_i + lastName.length.to_i
- puts 'Did you know there are ' + fullName.to_s + ' characters in your name?'
- # change the case (upper/lowercase) of a string
- letters = 'aAbBcCdDeE'
- puts letters.upcase
- puts letters.downcase
- puts letters.swapcase
- puts letters.capitalize
- puts ' a'.capitalize # only capitalizes the first character, not the first letter
- puts letters
- #visual formatting
- # .center the lines of a poem
- lineWidth = 50
- puts('Old Mother Hubbard'.center(lineWidth))
- puts('Sat in her cupboard'.center(lineWidth))
- puts('Eating her curs an whey,'.center(lineWidth))
- puts('When along came a spider'.center(lineWidth))
- puts('Which sat down beside her'.center(lineWidth))
- puts('And scared her poor shoe dog away.'.center(lineWidth))
- # left justify and right justify -> pad on either side
- # center, ljust, & rjust
- lineWidth = 40
- str = '--> text <--'
- puts str.ljust lineWidth
- puts str.center lineWidth
- puts str.rjust lineWidth
- puts str.rjust (lineWidth/2)
- # Write an Angry Boss program. It should rudely ask what you want. Whatever you answer, the Angry Boss should yell it back to you, and then fire you. For example, if you type in [I want a raise.],
- # it should yell back "WHADDAYA MEAN "I WANT A RAISE."?!? YOU'RE FIRED!!"
- puts 'Uh oh! The boss is angry today.'
- puts 'Tread carefully. . .'
- puts 'Let\'s see if you can land a raise.'
- puts 'Ask by typing: I want a raise.'
- puts 'Angry Boss says: "What do you want!?"'
- raise = gets.chomp
- puts 'WHADDYA MEAN "I WANT ' + raise.upcase + '" ?!? YOU\'RE FIRED!"'
- # Write a program which will display a Table of Contents so that it looks like this:
- # Table of Contents
- #
- # Chapter 1: Numbers page 1
- # Chapter 2: Letters page 72
- # Chapter 3: Variables page 118
- page_width = 60
- puts ('Table of Contents'.center(page_width))
- puts ''
- puts ('Chapter 1: Getting Started'.ljust(page_width/2) + 'page 1'.rjust(page_width/2))
- puts ('Chapter 2: Numbers'.ljust(page_width/2) + 'page 9'.rjust(page_width/2))
- puts ('Chapter 3: Letters'.ljust(page_width/2) + 'page 13'.rjust(page_width/2))
Add Comment
Please, Sign In to add comment