Definitions Flashcards
Defining common Ruby terminology.
What are Variables?
Any plain, lowercase word is a variable in Ruby. Consist of Letters, digits and underscores.
Example: x, y, banana2 or phone_a_quail
Define Numbers.
Basic type of number is an integer, a series of digits which can start with a plus or minus sign.
commas are not allowed in numbers, but underscores are. “Example: population = 12_000_000”
What are Decimal Numbers?
Called floats in Ruby. Floats consits of numbers with a decimal place or scientific notation.
Example: 3.14, -808.08 and 12.043e-04
What are strings?
Strings are any sort of characters (letters, digits, punctuation) surrounded by quotes. Single and dbl quotes are used to create strings.
Example: “sealab”, ‘2021’,
T or F: When you enclose characters in quotes, are they stored together as a single string?
True - Think of a reporter jotting this down. “I’m a lot wiser,” says Avril Lavigne. “Now I know what the business is like”
avril_quote = “I’m a lot wiser. Now I know what the business is like.”
What is a symbol?
Symbols are words that look just like variables. May contain letters, digits, or underscores. But they start with a colon.
Symbols are also lighweight strings. Usually used in situations where you need a string but you won’t be printing it to the screen.
What do symbols start with?
Start with a colon. Example: :a, :b, or :ponce_de_leon
How are symbols used?
Symbols are lightweight strings. Usually used where you need a string, but you won’t be printing it to the screen.
Example: You could say a symbol is a bit easier on the computer. Like an antacid. The colon indicates the bubbles trickling up from your computer’s stomach as it digests the symbol. Sweet, sweet relief.
What are Constants?
Constants are words like variables, but constants are Capitalized. If variables are the nons of Ruby, then think of constants as the proper nouns.
Example: Time, Array or Bunny_Lake_is_Missing
T or F: Do constants change over time?
False - Constants refer to something very specific and can’t be changed after they’re set.
Example: EmpireStateBuilding = “350 5th Avenue, NYC, NY”
What are Methods?
Used to bundle one or more repeatable statements into a single unit. Begins with lowercase letter. If variables & constants are nouns, then methods are the verbs. Methods are usually attached to the end of variable and constants by a dot.
Example: front_door.open
What are Method Arguments?
Method arguments are attached to the end of a method. Generally surrounded by parentheses and separated by commas.
Example: front_door.paint( 3, :red )
This paints the front door 3 coats of red.
Methods may require more information to perfom its action. Written Example: If we want the computer to paint the door, we should provide a color as well.
T or F: Can method arguments be chained?
True -
Example: front_door.paint( 3, :red ).dry( 30 ).close()
The above paints the front door 3 coats or red, dries for 30 minutes, and closes the door. The last parentheses are normally dropped.
What are class methods?
Class methods are usually attached after variables and constants. Rather than a dot, a double colon is used. Class methods are for anything that does not deal with an individual instance of a class.
Example: Door::new( :oak )
What are global variables?
Variables which begin with a dollar sign are global. Can be referred to from anywhere in a program. Should be used sparingly.
Example: $x, $1, $chunky and $CHunKY_bACOn
What are class variables?
Varaibles which begin with double at symbols are class variables. Also used to define attributes. They can also give an attribute to many related objects in Ruby.
Example: @@x, @@y and @@i_will_eat_chunky_bacon
The double at prefix means to attribute all. Or, you think of it as a swarm of AT’s-AT’s from Star Wars, which are all commanded by Ruby. If you change a class variable not just one changes, they all change.
What are Blocks?
Any code surrounded by curly braces is a block.
Example: 2.times { print “Chunky bacon!” }
You can group a set of instructions together so that they can be passed around your program.
Curly braces can also be traded for the words Do and End, which is nice if your block is longer than on line.
What are block arguments?
They’re sets of varibles surrounded by pipe characters and separated by commas.
Example: |x| , |x,y| , or |up, down, all_around|
They are used at the begining of a block.
Example: { |x,y| x+y }
What are ranges?
A range is two values surrounded by parentheses and separated by an ellipsis (in the form of two or three dots).
Example: (1..3) is a range, representing the numbers 1 through 3.
( ‘a’ .. ‘z’ ) is a range, representing a lowercase alphabet.
Think of it as an accordion which has been squeezed down for carrying.
Normally, only two dots are used. if a third do is used, the last value in the range is excluded.
What is an Array?
An array is a list surrounded by square brackets and separated by commas.
Example: [ 1, 2, 3,] is an array of numbers.
[ ‘coat’, ‘mittens’, ‘snowboard’] is an array of strings.
Arrays are a collection of things, but it also keeps those things in a specific order.
What are hashes?
A hash is a dictionary surrounded by curly braces. Dictionaries match words with their definitions. Ruby does so with arrows made from an equals sign, followed by a greater-than sign.
Example: { ‘a’ => ‘aardvark’, ‘b’ => ‘badger’ }
Second definition: A hash is a collection of key-value pairs. Similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. (hashes are sometimes called associated arrays.)
What are regular expressions?
A regular expression (or regexp) is a set of characters surrounded by slashes.
Example: /ruby/, /[0-9]+/ and /^\d{3}-\d{4}/
Regular expressions are used to find words or patterns in text. The slashes on each side of the expression are pins. Ruby can use a regular expression to search volumes of books very quickly.
What are Operators?
Operators are used to do math in Ruby or to compare things.
Example: ** ! ~ * / % + - & «_space;»_space; | ^ > >= < || != !~ && += -= == === .. … not and or
What are keywords?
Ruby has a number of built-in words with an ingrained meaning.
Example: alias and BEGIN begin break case class def defined do else elsif END end ensure false for if in module next nil not or redo rescue retry return self super then true undef unless until when while yield