Fundamentals Flashcards
Remember Ruby snytax
Variables
Any plain, lowercase word. Can consist of letters, digits, and underscores.
ex. ruby_123
Numbers
Integer or series of digits. No commas but underscores allowed.
ex. 1 or -23 or 12_000_000
Floats
Decimal Numbers or scientific notation
ex. 3.14 or 12.043e-04
Strings
Any sort of characters(letters, digits, punctuation) surrounded by quotes.
ex. “sealab” or “These cartoons are great!”
Symbols
Words that look like variables but start with a colon. Used in situations where you need a string but won’t be printing it to the screen.
Ruby responds quicker symbols then strings. (Pointers)
ex. :b or :ponce_de_leon
Constants
Words like variables but are Capitalized. Variables are nouns then Contstants are proper nouns. Contstants can’t be changes once set.
ex. Time or Bunny_Lake
Methods
Methods are verbs usually set to the end of a variable or contstant with a dot. They send a message. ! and ? Are allowed.
ex. front_door.open
Method Argument
Arguments attached to the end of a method. Usually surround by a parentheses and separated by commas and can be chained – inner tubes
ex. front_door.paint( 3, :red ) .dry( 30 ) .close()
Kernel Methods
Common methods that don’t use parentheses with arguments
ex. print “See, no dot.”
Kernel::print they are all class methods
p “See, no dot.” Also works for print
Class/Instant Methods
Rather than a dot a double colon is used after variable or constant
ex. Door: :new( :oak ) Class method is new
Global Variables
Begin with a dollar sign and good throughout your program no matter what class
ex. $1 or $chucky
Instant Variables
Begin with @ and define the attributes of something. Think @ means attribute
ex. front_door with @width
Class Variables
Double @@ used to define many related objects in a given class. @@ attribute all
ex. @@x is a class variable for all doors not just front_door
Blocks
Any code surrounded by curly braces. A group of instructions you can use “do” or “end”
Scope is all information before block.
ex. 2.times { print “yes win!” }
Block Arguments
Set of variables surrounds by pipe characters and separated by commas used at the beginning of a block. Tunnel in which variables enter the block
ex. |x,y|
Ranges
2 values surrounded by parentheses and separated by ellipsis
ex. (1..5) is 1 through 5 or (‘a’..’z’)
If 3 … Used the last range is excluded (0…5) is 0 through 4
Arrays
List surround by square brackets
ex. [1,2,5] or [‘coat’, ‘mittens’, ‘gloves’]
Hashes
Dictionary surrounds by curly braces because they can be easy to search through. Unlike an array hashes are not kept in particular order. Keys => Values
ex. { ‘a’ => ‘aardvark’, ‘b’ => ‘badger’ }
code_words[‘a’] will return aardvark