Skip to content

kaddae/variable-readme-001-prework-web

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction to Variables

##Objectives

  1. Define a variable.
  2. Create and reassign variables.
  3. Define pass-by-value as it relates to variables

Video

MP4

Variables in Ruby

Much like in math, variables are words or characters that hold values. In algebra, however, variables are only placeholders for numbers. In Ruby, a variable can point to almost any type of value including numbers, strings, arrays, and hashes.

Creating Variables

Variables are assigned values using = ("equal sign"), called the assignment operator. Variable names are typically all lower case and, in the case of multiple words, the words are separated by underscores.

current_president = "Barack Obama"
puts "In 2014, the president was #{current_president}."

The code above will print In 2014, the president was Barack Obama..

Note: The syntax of #{current_president} simply injects the value of the variable current_president into the string. This is called Interpolation and we'll cover it later - but think of it as "In 2014, the president was" + current_president where you are adding that value to a string.

Reassigning Variables

Now the variable current_president is equal to the string Barack Obama. Let's say somehow Stephen Colbert got elected as president for 2016. To update current_president, you would just reassign the variable much in the same way that you first defined it:

current_president = "Barack Obama"
puts "In 2014, the president was #{current_president}."

current_president = "Stephen Colbert"
puts "Now, it being the year 2016, the president is #{current_president}."

This will print out:

In 2014, the president was Barack Obama.
Now, it being the year 2016, the president is Stephen Colbert.

Variable Example

Within this repository is a file named variables.rb with some examples you can read and play with.

'This is data, it is a string. Strings start with " "'

"Part of being data, or a string, is that ruby doesn't interpret it."

puts 1+1
puts "1+1"

example = "The word 'example' is equal to this sentence, it's a named variable."

puts example
puts example
puts example

puts "variables are any previously undefined word that"
puts "starts with a lowercase letter."

Running this file will print:

2
1+1
The word 'example' is equal to this sentence, it's a named variable.
The word 'example' is equal to this sentence, it's a named variable.
The word 'example' is equal to this sentence, it's a named variable.
variables are any previously undefined word that
starts with a lowercase letter.

Resources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages