Giter Site home page Giter Site logo

programming-univbasics-the-assignment-expression-lax-web-033020's Introduction

The Assignment Expression

Learning Goals

  • Define the Assignment Expression
  • Define Mutability / Immutability
  • Constant versus "Symbolic Constants" and "Values"
  • Return Value of an Assignment Expression

Introduction

So you've seen the first of our essential three expressions, the constant expression which gives Ruby some constant fact about the world: 2 is 2.

It's really useful to associate an expression's evaluated result with a 'name'. We call those names that we associate with the expression's result, variable names or, commonly, just variables. The process of bonding an expression to a variable is called assigning a variable. Programmers also say that "the variable name 'points to' the expression that was assigned to it."

Some helpful metaphors here are it's like adding a new entry to a dictionary a_fun_number's definition is 3 * (10 - 4) or my_birth_year is 1929.

Variable naming as dictionary entry

Or a variable name is like a label you put on a box:

Variable naming as labeled box

We use the second of our essential three expressions to do this: the assignment expression.

Define the Assignment Expression

In Ruby, the assignment expression is like so:

Assignment Expression Graphic

Here are some examples:

a_fun_number = 3 * (10 - 4)
my_birth_year = 1929

Variables names are most often lower case descriptions of what their assigned expressions mean. When the words need to be separated we use _ to act like a "space". While you can include some numbers and symbols as variable names, let's keep things simple for the moment and just use lower-case letters and _.

When we define a variable using the "assignment expression" we add something new to Ruby itself.

Consider this IRB conversation:

irb(main):001:0> maximum_speed
NameError (undefined local variable or method `maximum_speed' for main:Object)
irb(main):002:0> maximum_speed = 55
=> 55

Here, Ruby, by default, doesn't know anything about maximum_speed. After "defining the variable maximum_speed, it did.

SUPER-IMPORTANT: In the assignment expression = means "assignment" it does not mean "what's on the left of the = is equal to what's on the left." In math courses, we use = to say that the expressions on either side of the = are the same. Ruby uses == for that purpose. It's very confusing for beginners to have bugs where they confuse = for ==.

Define Mutability / Immutability

A variable is said to be "mutable." That means the value that the name "points to" can be changed during the running of the program. Being able to change the value a variable points to is very important. If we need to do something 10 times or until the page loads, we need to be able to count how many times something happens. Here's "mutability" in action:

Many years ago I was:

height_in_centimeters = 50

But today I am:

height_in_centimeters = 180

We can try these out in IRB:

Variable Asignment

Sometimes, we might want to make a variable name permanent. We might want to say "hey, this value should not change." We want to say that the value is immutable, the opposite of mutable. We do this by writing a "constant." In Ruby, constants are assigned just like variables, except their names are written in ALL_CAPS.

SPEED_OF_LIGHT = 180000

If, after this, you try to set SPEED_OF_LIGHT to something else, Ruby will complain.

SPEED_OF_LIGHT = 1800000
warning: already initialized constant SPEED_OF_LIGHT

Constant Assignment

Programmers call an assignment to a variable that is meant to be "not-change-able" or immutable, "setting a constant."

Sometimes we want the programming language to yell at us if we try to change the value of certain variables. In real life, changing the value of the speed of light would be very bad for existence in this universe. If we had a program for doing space travel written in Ruby, we might want it to yell at us if we tried to change this constant's value.

Return Value of an Assignment Expression

It's interesting, but the return value of an assignment expression is the evaluated result of the expression to the right of the =.

recurring_expression_value = 3 * (10 - 4)
=> 18

Pay attention here, the return value of the assignment expression IS NOT THE SAME THING as getting the value out of the variable name. We'll learn to get the variable "back out of a variable" in the next lesson. What Ruby's saying is that the assignment expression's return value is the value of the expression to the right of the =.

Conclusion

Think about a baby who has never spoken before. Before it, stands a parent saying their name over and over (...and over) again.

Learning to talk 1

They wave towards their bodies and say their names again and again. What the parent is trying to do is teach the baby to assign their face to the variable name "Mama" or "Dada." But to the baby, this means nothing.

While neither the baby or the (average) adult is aware of it, they're trying to teach the baby the second of the three essential expressions: the assignment expression. Then, one magical day, it clicks for the baby. It performs an assignment in its precious little head:

Learning to talk 2

Unfortunately, Mom is still sad, she doesn't have any proof that the assignment was successful. For that to work, the baby will need to prove that it can "look up" the variable assignment of who "ma-ma" points to. We'll need to teach Ruby, and baby will need to learn the last of our essential expressions: the variable lookup expression!

programming-univbasics-the-assignment-expression-lax-web-033020's People

Contributors

maxwellbenton avatar sgharms avatar

Watchers

 avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.