Tech

More Notes!!! [Ruby Programming Language Notes]

Global Variables–Variable References Lecture

$–Use this sign to create a global variable in Ruby.

Ex:

  • def say_hello
  • $message = “hello globe”
  • end
  • say_hello
  • p $message # => “hello globe”

More Methods!

  • object_id
    • can be used to see what memory location a particular variable points to .
    • ex: variable.object_id
    • => 7018903, or whatever number the computer assigned.

Initializing an Array! *(Variable References Lecture)

So we know that ‘arr = [ ]‘ will create a new empty array. We can use ‘arr = Array.new()‘ to initialize an array of a certain length by passing the length we want into the parameter (argument).

ex:

  • Array.new(3) #a new array with 3 elements
  • => [nil, nil, nil] (Array of length (3), with no values.)

Also, we can decide the default values as something other than nil!

  • ex: arr = Array.new (3,”?”) #’3′ is the numerical quantity of elements; a the second set is the values…in this example a string of “?”‘s.
  • => [“?”,”?”,”?”]
  • We can assign any default value we want!
    • Ex: arr = Array.new(3,0)
    • => [0,0,0]
  • We can also create an array of arrays aka sub-arrays!
    • arr = Array.new(3,[ ])
    • => [ [ ], [ ], [ ] ]

Initializing Arrays

We can also do this;

Ex:

  • grid = Array.new(3, Array.new(3) )
  • #This will create an array of 3 elements where each element is a subarray of 3 elements (with no values assigned in this particular example.)
  • => [ [nil,nil,nil], [nil,nil,nil], [nil,nil,nil] ]
    • (This could be used to create something like a 3×3 grid for Tic-Tac-Toe!)
    • EXCEPT if we index into this, it’s not fully ready because it would create a classic variable reference mistake:
    • Ex: Indexing into the previous example would give us this:
    • grid[0][0] = “x” #if we change this index
    • p grid
    • => [ [“x”,nil,nil] , [“x”,nil,nil] , [“x”,nil,nil] ]
    • (Notice it changed all 3 of the sub-arrays, instead of just the first sub-array.)
    • This is because when we started:
      • grid = Array.new(3, Array.new(3) ) #This creates the outer array.
      • BUT as the default value, we passed in this sub-array value: ‘Array.new(3)‘.
      • It went in as the default value for each of the sub-arrays.
      • Each of the sub-arrays is really pointing to, or referencing!) the same value (or location!) in the computers memory! So a change will change all of them!

To combat the problem in the previous example, Ruby has another syntax we can use to make the expression work as we want by not changing all the sub-arrays at once.

So: grid = Array.new(3, Array.new(3) ) #This is passing in a single array as the sub-array all pointing to the same location, instead of many unique arrays in the computers memory, which is what we actually want in this particular case.

So instead, we can use another syntax by dropping the 2nd argument and passing in a block of code!

  • grid = Array.new(3) { }
    • So in the above, ‘Array.new(3)’ just creates an array of length ‘3’.
    • Then, in the curly braces, (or block of code!), we can write an expression that evaluates to the default value that we want. So:
      • grid = Array.new(3) {Array.new(3)}
      • By passing the same expression as a block of code in curly braces, the key difference this time is that a block contains code yet to be executed!
      • So now the outer array will evaluate the block of code multiple times, each time creating a new (or unique) array!
    • So now while: ‘p grid’ will still show us:
      • => [ [nil,nil,nil] , [nil,nil,nil] , [nil,nil,nil] ]
    • The result seems to look the same as before BUT, now if we modify a single sub-array:
      • Ex: grid[0][0] = “x”
    • We will now get:
      • p grid
      • => [ [“x”,nil,nil] , [nil,nil,nil] , [nil,nil,nil] ]
      • Because these are now 3 distinct (unique) sub-arrays, now only the first sub-array changed.
    • To recap, if we want an array of unique sub-arrays with a default initial value, we need to use a block, { }, instead!, because the block contains code that will give us a new sub-array every time it’s executed!