Alright time for some conditionals! But before that we need to get a few things down.
true,false and nil
true
, false
and nil
are like everything else in ruby are objects that
have their own classes in which they are the only objects. true
and false
are both native boolean values. You could set it to variables, use it for
methods and what not.
|
|
nil
is something different altogether. Nothing is a weird thing in programming
and different thing has different programming languages. nil
is not zero, zero
is a value, it is something but nil
is nothing like the absence of anything.
|
|
Comparative and Logical operators
Ruby has the following logical operators: && || !
. You could also use the
words and or not
and they do what you think they'd do.
|
|
We also have the following comparative operators: \== != < > <= >= Those also do what you'd expect them to do. You could see them in action when we finally get to conditionals in the next section.
Conditionals
Let's say we have a variable called version
and wish to print out stuff based
on the value of version
, then this is how we would do it.
|
|
Now if you run that it'll look something like this:
|
|
Now what if want to check for multiple conditions?
|
|
And executing that will give us this. By the way you can make ruby scripts by
putting .rb at the end of the filename and as you can see you can run scripts by
providing the filename as a parameter to the ruby
command.
|
|
There are more fun things that we could do with these little words, but it's late here and I'm gonna probaby do that on Day 4.