In Ruby, string interpolation is used to insert the result of Ruby code into a string.
age = 30print "Hi, my name is Cody, and I am #{age} years old"# "Hi, my name is Cody, and I am 30 years old"
In Ruby, we can get the user’s input using gets.chomp
. gets
is the method that is used to retrieve user input. Ruby automatically adds a new line after each bit of input, so chomp
is used to remove that extra line.
print "Type your name and press Enter: "name = gets.chompputs "My name is #{name}!"
In Ruby, a variable is a place to store values of almost any type including Integer, 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
.
my_var = 48
put
and print
commands can be used to display text in the console.
print "Hello"puts "This is written in a new line"print "Still printing"
Commenting code helps programmers write free text that is commonly used to explain the code written, or can even be used to add TODO’s to the code. There are two types of comments that can be written in Ruby:
#
.=begin
and end with =end
.# I am a single line comment.=beginI am a multi line comment.I can take as many lines as needed.=end
In Ruby, the Numeric data type represents numbers including integers and floats.
# Integer valuex = 1# Float valuey = 1.2
In Ruby, we can use arithmetic operators to evaluate mathematical expressions. The most common Ruby arithmetic operators are addition (+), subtraction (-), division(/), multiplication(*), exponentiation(**) and modulo(%).
print 1+3# Addition: output 4print 1-2# Subtraction: output -1print 9/3# Division: output 3print 2*3# Multiplication: output 6print 2**3# Exponentiation: output 8print 16%9# Modulo: output 7
In Ruby, methods are built-in abilities of objects. To access an object’s methods, you need to call it using a .
and the method name.
var = "codecademy"# Method to get the length of a stringprint var.length # 10# Method to get the string reversedprint var.reverse # ymedacedoc# Method to convert all letters to uppercaseprint var.upcase # CODECADEMY
Strings in Ruby are a sequence of characters enclosed by single quotation marks (‘’) or double quotation marks (“”).
# String 1s1 = 'I am a single string!'# String 2s2 = "I am a double string!"
In Ruby, in order to represent values of truth about specific statements, we use Boolean variables. Boolean variables values are either true
or false
.
# Boolean true variableyear2019 = true# Boolean false variableyear2018 = false
.upcase
and .downcase
MethodsRuby strings have an .upcase
and a .downcase
method used to change their case. .upcase
returns a version of the string in all uppercase, and .downcase
returns a version in all lowercase.
puts "codecademy".upcase# CODECADEMYputs "Codecademy".downcase# codecademy