understand-variable

Reading time ~3 minutes

I write this post as my first blog because I failed my first assessment. I am frustrated first, then I thought, yes, there must be something that I don’t understand very well. So I spend an afternoon to figure out what’s going wrong. I found there are few things that I mess up.

  1. variable and objects
  2. inner scope/ outer scope
  3. method mutate/non-mutate
  4. true and evaluate to be true

I do pretty much exercise without understanding easy but important concepts. I am glad that I figure it out now. I am never aware that I do not understand all of this at all.

About variables ===============

To answer my first question here, I follow the references and mutability of Ruby Object. This article totally answers my first question there. Here is what I learn from it:

Variables are used to store information to be referenced and manipulated in a computer program.

  • a way of labeling data with a descriptive name
  • containers that hold information
  • label and store data in memory

reference_1 reference_2 reference_3 reference_4

Variables don’t contain values, but instead, serve as references to objects . Variables are like a label of an object. Therefore, you can call the object using the variable. Just like your name or nickname. Or you name your laptop with “little mac”, your laptop is an object, and the nickname you give your laptop is a variable.

variable scope

I have some issue about the scope. I do know that inner scope is different that outer, and I also understand that variable can pass from outside to inside, but not vice versa.

But I mess up with variable and object. Previously, I thought the scopes are using to identify a different object. In fact, the scope has a adjective before it, it calls variable scope .

  • A variable’s scope determines where in a program a variable is available for use
  • variable scope is defined by a block.
    • A block is a piece of code following a method
    • usually delimited by either curly braces {} or do/end
  • Inner scope can access variables initialized in an outer scope, but not vice versa.

Methods create their own scope that’s entirely outside of the execution flow.

Example:

  a = 5

  def a_method
    a = 3
  end

  puts a # 5

  3.times do |n|
    b = 10
  end

  puts b # unable to access

If variable is initialized in inner scope, it can not be accessed in outer scope.

passed by reference or pass by value?

Immutable objects still seem to be passed by value, while mutable objects seemed to be passed by reference. we can say *“pass by value”* for immutable objects, and pass by reference for mutable objects .

Assignment does not mutate objects but instead binds variables to new objects, while setter methods and indexed assignment do mutate objects

  • Ruby variables are merely references to objects in memory
  • variable is merely a name for some object

About method


  def take(something)
    return something
  end

  take("a ball")

take is a method in ruby, and something is a parameter which we use to pass outer argument. we pass “a ball”, String as arguement, and then “a ball” is assign to something.

mutating and non-mutating method

All methods are non-mutating with respect to immutable objects. A method simply can’t modify an immutable object. Thus, any method that operates on numbers and boolean values is guaranteed to be non-mutating with respect to that value.


  def change(word)
    puts "after pass argument to method #{word.object_id}"
    word = word.upcase
    puts "Let's see is the same object after upcase? #{word.object_id}"
    word.concat("?")
  end

  greeting = "world"
  p "here is the initialized object: #{greeting.object_id}"
  p greeting
  another_greeting = change(greeting)
  p "the final object is #{another_greeting.object_id}"
  p another_greeting

I use four chart to describe the code.

mutate_1 mutate_2 mutate_3 mutate_4

object passing strategy

Ruby variables and constants aren’t objects, but are references to objects. Assignment merely changes which object is bound to a particular variable. Ruby passes around copies of the references. In short, ruby is neither pass by value nor pass by reference, but instead, employs a third strategy that blends the two strategies.

to_param in Ruby on Rails

If I want a custom slug=======================I walk through this cutstom slug.1. create migration `add_slug` to add a column inside the...… Continue reading

What is ORM in Rails mean?

Published on July 14, 2017