Learning Ruby Core Concepts Part 1

Adriana - Aug 20 - - Dev Community

Values of Variables Can Be:
Numeric: Any Number/Numerical
Boolean: True or False values (These are written in lowercase! Do not use “” as the computer will assume the value is string instead of Boolean)
String: Uses “” to contain word or phrase values.

An example of a Numeric Variable Value can be:
my_num = 25 (Note the capitalization, or lack thereof)
You can reassign a variable by using the same structure → my_num = 30

Ruby Math has multiple operators:
Addition (+)
Subtraction (-)
Multiplication ()
Division (/)
Exponentiation (
) (2*3 Would have an output of 8 since it reflects 2*2*2)
Modulo (%) (25 % 7 would be 4, since 7 goes into 25 three times with 4 remaining)

Example:
Input:
sum = 13 + 79
product = 923 * 15
quotient = 13209 / 17

puts sum
puts product
puts quotient

Output
392
13845
777

Puts/Print Command
Print command prints whatever inputs are provided, Puts (For “put string) adds a new (blank) line after the item(s) you want it to print.

Input: puts “What’s up?”
print “Hey”

Output: What’s up?

             Hey
Enter fullscreen mode Exit fullscreen mode

Ruby is an object oriented language that includes methods that you can write in an editor the interpreter runs said code.

.length method returns the value of the number of characters, spaces, and numbers of a string
In order to have the console reflect the answer, write the function like so:

Input: puts “I love cake”.length
Output: 11

.length is helpful for certain value input fields such as a credit card or phone number

.reverse method returns the string value backwards or in reverse

Input: puts “Cheese”.reverse
Output: “essehC”

.reverse is useful for sorting lists from greatest to least or vice versa

.upcase and .downcase are methods that convert a string to upper case and lower case respectively

Input: puts “juice”.upcase
Output: JUICE

Input: puts “JUIce”.downcase
Output: juice

Single Line Comments
-The ‘#’ sign is used for comments in Ruby. The program will not run anything in this line of text. The comment section comes after the ‘#’ sign and won’t affect the code as long as it is on a single line

Input: # I’m a full line comment!
puts“Eric”.length # I’m a comment, too!
Output: 4

Multi-Line Comments
To start a multi line comment, use ‘=begin’ and end with ‘=end’
=begin
I’m a comment!
I don’t need any # symbols.
=end

In Ruby, you can write each method on a separate line or chained together:
( variable.method1.method2.method3)
Remember to include ‘puts’ for the methods to see the result!

Input: name = “Adriana”
puts name.downcase.reverse.upcase
Output: ANAIRDA

.gets is a method that gets input from the user. When the input is received, the output automatically adds a blank (new) line, .chomp removes that extra line, but the code will still work without using the method.

Input:
prints = “What’s your first name?”
first_name = gets.chomp
Output:
What’s your first name? (You can type a response into the terminal code *NOT the editor)

Input:
print "What's your first name? "
first_name = gets.chomp.capitalize!

print "What's your last name? "
last_name = gets.chomp.capitalize!

print "What city are you from? "
city = gets.chomp.capitalize!

print "What state or province are you from? "
state = gets.chomp.upcase!

puts "Your name is #{first_name} #{last_name} and you're from #{city}, #{state}!"

Output:
What's your first name? adriana
What's your last name? lopez
What city are you from? chicago
What state or province are you from? il
Your name is Adriana Lopez and you're from Chicago, IL!

‘If’ statements, evaluate a value to be either true or false (not to be confused with Boolean). If the expression is true Ruby executes the block of code. If the expression is false, it moves on to execute the next block of code. ‘If’ statements always finish with ‘end’
‘else’ is the next condition if the previous ‘if’ statement does not execute as ‘true’
‘elsif ’ works in a similar way to ‘else’

Input:
if 1 > 3
prints “True”
else
Prints “False”
end

Output:
False

‘==’ statement checks to see if variables previously defined are equal to each other (true). To check if values are not equal to each other (false) then use ‘!==’
X =2
Y = 2
if x==y
prints “X is equal to Y”
end

Comparison Operators:

Less than
=> Less than or equal to
< Greater than
<= Greater than or equal to

test_one = 17 > 10

Logical/Boolean Operators
The Boolean operator and (‘&&’) only results in ‘true’ if both sides of an expression are true

Input:
true && true
true && false

Output:
True
False

The or (||) operator is an inclusive or because it evaluates to true when one or both of the expression are true

Input:
true || true
true || false
false || true
false || false

Output:
True
True
True
False

The boolean operator not (‘!’) makes true values false, and false values true
Example Goes Here

You can combine Boolean Operators in an expression:

Input:
boolean_1 (3 < 4 || false && (false || true)
Result:
boolean_1 = true

Operator Example:
daytime = false
print “It is nighttime” unless daytime

User Input:
prints “How old are You?”
user_input = gets.chomp

This allows a user to type a response into the terminal for the system to process

The ‘include?’ method checks to see if a value is true (or false) based off of a condition.
(i.e. if user_input.include? “hello”) This allows a if/end statement that can access string variables (including sole letters, words, or phrases)

The ‘.gsub!’ method (Global substitution) will replace part of a string with something else
(i.e. user_input.gsub!(/hi/, “hello”) This will replace all strings that say ‘hi’ with ‘hello’

print "Pleathe enter a thtring: "
user_input = gets.chomp
user_input.downcase!

if user_input.include? "s"
user_input.gsub!(/s/, "th")
else
print "nothing here"
end
puts "I am, #{user_input}!"

This code changes an ‘s’s’ to ‘th’ and relays the message “I am__” with the user’s input (including modifications → downcase and ‘th’)

.
Terabox Video Player