recitation3 notes

2502125 useafloatasatleastoneoperandtostoptruncation

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: bles a. An instance variable is a variable that is referenced via an instance of a class and therefore belongs to a given object. b. It is always prefixed by a single at (@) sign c. Example: @hello = 6170 d. You can access instance variables from outside of their class only by accessor methods 4. Class Variables a. A instances of a class. Only one copy of a class variable exists for a given class. b. It is always prefixed by two at signs (@@) c. You have to initialize a class variable before you use it d. Example: @@times = 0 5. Global Variables a. Global variables are available globally to a program, inside any structure. Their scope is the whole program. b. It is always prefixed by a dollar sign ($) c. Example: $amount = “0.00” Topic 6: Math 1. Converting Numbers a. Integer(1.4) => 1 b. Integer(“256”) => 256 c. Integer(“0b100”) => 8 d. Integer(?z) => 122 // converts a floating-point number // converts a string // converts a binary number from string // converts a character to ASCII 2. Basic Math Operators: a. Use + , - , * , / for addition, subtraction, multiplication, and division 5 b. Use ** for exponent c. Use % for modulo 3. Division and Truncation a. 25 / 2 => 12 // Integer division always rounded down b. 25.0 / 2 => 12.5 // Use a float as at least one operand to stop truncation 4. Ranges a. The range operator . . means an inclusive range of numbers. Example: 1 . . 10 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) b. The range operator . . . means a range of numbers excluding the last one Example: 1 . . . 10 = (1, 2, 3, 4, 5, 6, 7, 8, 9) c. Creating an array of a range of numbers: (1 . . 9).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9] //.to_a means “to array” 5. Useful Math methods a. Absolute value: -40.abs => 40 b. Ceiling: 4.65.ceil => 5 c. Floor: 4.65.floor => 4 d. Rounding: 100.45.round => 100 Topic 7: String 1. Basic string functions: a. title = String.new b. title.empty? c. title.length // sets title = “” // returns boolean showing whether title is empty (“”) // returns length of title 2. Concatenating Strings a. “Hello,” “ “ “Matz” “!” b. “Hello,” + “ “ + “Matz” + “!” c. “Hello, “ << “Matz!” d. “Hello, “.concat “Matz!” // Creates “Hel...
View Full Document

Ask a homework question - tutors are online