These free mini-courses will give you a strong foundation in web development. Track your progress and access advanced courses on HTML/CSS, Ruby and JavaScript for free inside our student portal.
Scroll down...
This lesson is a mismatched batch of useful knowledge that doesn't have a home anywhere else.
So What is nil
? It represents nothing... literally. Before you assign a value to something, it starts as nil
, for instance an item in an array or a variable:
> my_arr = []
#=> []
> my_arr[3]
#=> nil # hasn't been assigned yet
Sometimes you want to know if a value or variable is nil
before doing something (because otherwise the operation would throw a bunch of errors at you). Use the method nil?
to ask whether it's nil or not beforehand.
> nil.nil?
#=> true
> [].nil?
#=> false # Waitasecond....
Why is []
not nil?
It isn't nil
because the array itself exists... it just happens to contain no values yet so it's empty. If we asked for the first value of that array using [][0].nil?
, that would be true
If you try to run a method on something that is nil
, which you will inevitably do many many times by accident, you'll get the familiar NoMethodError
:
> user_i_looked_up_but_was_not_found_so_is_nil.empty?
#=> NoMethodError: undefined method `empty?' for nil:NilClass
blank?
and empty?
are similar to each other -- both basically check if the object has nothing in it -- but blank?
will also ignore any whitespace characters. Note that blank?
is a method provided by Rails and is not available in Ruby.
We've seen lots of puts
so far but you've probably also run across p
. What's the Difference?
p
will give you some more information because it runs the inspect
method on the object while puts
runs the to_s
method. inspect
is meant to be informative where to_s
is "pretty". They look pretty much identical for now:
> h = {i: "hi",j: "go"}
#=> {:i=>"hi", :j=>"go"}
> puts h
{:i=>"hi", :j=>"go"}
#=> nil # returns nil
> p h
{:i=>"hi", :j=>"go"} # hmm.. still the same for now
#=> {:i=>"hi", :j=>"go"} # returns the object
The difference may not be readily apparent while you're only working with simple objects like strings and arrays, but you'll notice it when you start creating your own objects and you want to see what's inside (without typing out puts my_object.inspect
).
print
, on the other hand, is basically identical to puts
but ignores the newline at the end.
> puts "hi"; puts "hi" # ; designates a new line of code
hi
hi
#=> nil
> print "hi"; print "hi"
hihi #=> nil
They are slightly different with things like arrays, where print
will literally reprint the object while puts
takes liberties to prettify it (with each item on a new line):
> puts [1,nil,2]
1
2
#=> nil
> print [1,nil,2]
[1,nil,2] #=> nil
Point being, you'll probably just stick with puts
and that's fine.
=
is an Assignment Operator but there are a few others that are interesting and common shorthands as well:
a += b
is the same as a = a + b
a -= b
is the same as a = a - b
a *= b
is the same as a = a * b
a /= b
is the same as a = a / b
a %= b
is the same as a = a % b
a **= b
is the same as a = a ** b
Parallel Assignment is when you assign the values of more than one variable at a time (though it works for arrays as well!):
> a, b = 1, "hi"
#=> [1, "hi"] # ignore this output
> a
#=> 1
> b
#=> "hi"
> my_array = [1,2,3,4]
#=> [1,2,3,4]
> my_array[1], my_array[3] = 100, 200
#=> [100,200] # ignore
> my_array
#=> [1,100,3,200]
It's also a great way to Swap Two Variables:
> a = 10
> b = 20
> a,b = b,a
> a
#=> 20
> b
#=> 10