Programming Back
IO Operations
- To use
puts
to simply writes onto the screen whatever comes after it.
- To use
gets
to get words form the keyboard.
- To use
chomp
to take off the line break.
puts 1 + 2
puts 3
puts gets
let name = gets
puts 'Hello, ' + name + ', is that OK?'
let name = gets.chomp
puts 'Hello, ' + name + ', is that OK?'
Integers and Float
5
-205
99999999999999999999
0
54.321
0.001
-205.3884
0.0
Letter
puts 'Hello, Aleen!'
puts 'Hello, ' + 'Aleen!'
puts 5 * 5
puts '5' * 5
puts '5 * 5'
Variables and Assignments
let = 'variables'
puts let
let = 5 * (1 + 2)
puts let
Conversion
let1 = 2
let2 = '5'
puts let1 + let2
puts let1.to_s + let2
puts '15'.to_f
puts '99.99'.to_i
Methods
puts 'What\'s ' + 'up?'
puts 'What\'s '.+ 'up?'
puts 'What?'
self.puts 'What?'
puts `pig` * 5
let1 = 'stop'
puts let1.reverse
let2 = 'Aleen'
puts 'There\'re ' + let2.length.to_s + ' characters in ' + let2
letters = 'aAbBcCdDeE'
puts letters.upcase
puts letters.downcase
puts letters.swapcase
puts letters.capitalize
let3 = 'Alien'
lineWidth = 50
puts let3.center(lineWidth)
puts let3.ljust(lineWidth)
puts let3.rjust(lineWidth)
puts rand(101)
puts srand(1776)
puts srand(1776)
puts Math::PI
puts Math::E
puts Math.cos(Math::PI / 3)
Flow Control
puts 1 > 2
puts 1 < 2
if name == 'Aleen'
puts 'Hello, ' + name + '!'
else
puts 'Who are you, ' + name + '?'
end
commands = gets.chomp
whild commands != 'exit'
puts commands
command = gets.chomp
end
puts true and false
puts true or false
puts not(true)