Comptuer Studies Ruby Notes

838 Words Mar 25th, 2013 4 Pages
These are notes I took while I was learning Ruby.
Comptuer studies ruby notes
Instance of a class is a sub-category of that class. E.g. greyhound; dog. Every object has a class. Objects are instances of classes. Methods define what an object can do and properties describe it.
Constants start with a capital letter, sometimes they are all caps e.g.
INTEREST = 0.012 #setting constant INTEREST to 1.2%
DOZEN = 12 #setting constant DOZEN to 12
Constants and variables store information in the memory for the duration of the use of the program. Like RAM?
Constants can’t be changed but variables can be reassigned based on certain properties and data.
Different types of classes:
Class Example of Object
Float 6.5 or 3.9
Fixnum 2 or 3
…show more content…
(e.g. @@fav_food)
Instance variables are restricted to only certain instances of a class. If the value changes in one of the instances it stays the same in the others. (e.g. @fav_food)
Commands to determine classes
puts variable.class or puts variable.kind_of? Class
The first will tell you what class the variable is while the other will say true or false based on what class is inputted at the end of the line.
Changing classes
The easiest way is to just assign a new value to it.
Another way is to use the to_s, to_i etc. commands while also using the “=” assignment operator in order to change the object’s class permanently
num = num.to_s you can do it without the “=” and the num variable before the equals sign in order to change it temporarily
All math operations are the same except for two. Modulo (%) Divides and gives the remainder and exponent is (**). E.g:
X = 6%4
X = 20 ** 2
There are also comparison operators such as: puts a == b #false as a and b are not equal puts a != b #true as a and b are not equal puts a < b #returns true as b is larger

More about Comptuer Studies Ruby Notes

Open Document