Learn Ruby - Introduction To Ruby Cheatsheet - Codecademy
Learn Ruby - Introduction To Ruby Cheatsheet - Codecademy
Introduction to Ruby
Ruby Variables
In Ruby, a variable is a place to store values of almost any type including Integer, my_var = 48
Boolean, String, Array, and Hashes.
Each variable has its own name which cannot begin with a capital letter or a number
and we use the equal sign for assigning a value to that variable.
The variable declaration does not require that you mention a specific data type.
The following program declares my_var variable and assigns the value 48 .
# Float value
y = 1.2
print 1-2
# Subtraction: output -1
print 9/3
# Division: output 3
print 2*3
# Multiplication: output 6
print 2**3
# Exponentiation: output 8
print 16%9
# Modulo: output 7
Ruby Object Methods
In Ruby, methods are built-in abilities of objects. To access an object’s methods, you var = "codecademy"
need to call it using a . and the method name.
Strings in Ruby
Strings in Ruby are a sequence of characters enclosed by single quotation marks (‘’) or # String 1
double quotation marks (“”).
s1 = 'I am a single string!'
# String 2
s2 = "I am a double string!"
puts "Codecademy".downcase
# codecademy
Print Share