Monday, May 13, 2013

Ruby Lesson 1 Section 1 of 4: Variables and Data Types

Section Topics:

  • Data Types: Numbers, Booleans, Strings
  • Variables 
  • Math
  • puts and print

There are 3 Data Types: Numbers, Booleans, and Strings

Numbers - In the example problem it was asked to input the number 25. This leaves several questions as to whether it needs to be a whole positive number, or can it be any number in particular (negatives, integers, or fractions). Through some testing it does seem like all of the above are acceptable inputs for a number.

Booleans - Are either set equal to True or False.

Strings - This is any word(s) or number(s) in quotations. Quotes can be written in either ( ' or " ) form. Anything written written with quotes around them Ruby will take as a string.

Update (May 14, 2013): Ruby will error if you mix and match the quotation marks for a string. 

  1. Input: print 'hello"

    Output: error


A person sets a variable by stating what they want to call the variable and assigning it a value. Code academy gives me a number and it doesn't seem like Ruby will take anything other than numbers.

Ruby also does not take spaces in variables so underscores "_" are used instead.

  1. cats = 3
    dogs = 4

    Input: cats + dogs
    Output: 7
  2. my_num = 100

    Input: my_num
    Output: 100
  3. my num = 100

    Input: my num
    Output: error 
But it seems that spacing does not matter in setting variables when it's between the variable and the equal sign and between the equal sign and the assined value. For instance the following variables all output 100.

my_num = 100
my_num= 100
my_num =100


This is fairly straight forward. There are 6 operators:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Exponentiation (**)
  6. Modulo (%)
The exponentiation is usually taught to us as "^".  As in 2^3 = 8

Example in Ruby:
  1. Input: 2**3
    Output: 8
The Modulo returns the remainder of a division. For instance 10/7 would give us a remainder 3.

Example in Ruby:
  1. Input: 10%7
    Output: 3
Update (May 14, 2013): Testing the division operator it seems that Ruby will only return whole numbers unless a you add a value in the tenth place, just adding a decimal will not do. As you can tell, without knowing this a person's calculations can be off by quite a bit. 

  1. Input: 3/2
    Output: 1
  2. Input: 3./2
    Output: 1
  3. Input: 3.0/2
    Output: 1.5

puts and print

Now there's a reason why puts and print are undercased. Ruby is case sensitive and as far as I can tell if you capitalize them Ruby will return an error. 

puts and print are both Ruby commands that outputs a string. 

  1. Input: puts "hello"
              print "good bye"

    Output: hello
                 good bye
What is the difference between puts and print? I struggled with this the first time even with Code Academy's explaination. puts adds a blank line after the string while print does not. 

  1. Input: print "hello"
              print "good bye"

    Output: hellogood bye
  2. Input: puts "hello"
              puts "good bye"

    Output: hello
                 good bye
In addition is seems that if you can't put more than 1 command on a line. I guess that's why they call it a command line instead of a command(s) line. hahaha! (lame I know). 

No comments:

Post a Comment