Ruby Basics Flashcards
literal
Any notation that lets you represent a fixed value
‘Hello, world!’ // string literal
375 // integer literal
3.141528 // float literal
true // boolean literal
{ ‘a’ => 1, ‘b’ => 2 } // hash literal
[ 1, 2, 3 ] // array literal
:sym // symbol literal
nil // nil literal
string
list of characters in a specific sequence
written with either single or double quotes
if using single quotes, then single quotes within the text are indicated with escaping: ‘The man said, 'Hi there !' ‘
double quotes allow string interpolation
:symbol
indicated with leading “:”
Used to reference something like a string, but don’t intend to print to screen or change.
Referred to as immutable, although that’s not technically correct
numbers
Integer (whole numbers)
Float (with decimals)
nil
indicates “nothing”
can check for nil with .nil?
modulo, remainder, divmod
Return either modulus or an array containing the quotient and modulus
num.divmod(numeric) → [quotient, modulus]
num % divisor → remainder
num.remainder(numeric) → remainder
a b a % b (modulo) a.remainder(b) a.divmod(b) 17 5 2 2 [3, 2] 17 -5 -3 2 [-4, -3] -17 5 3 -2 [-4, 3] -17 -5 -2 -2 [3, -2]
modulo, remainder, divmod
Return either modulus or an array containing the quotient and modulus
num.divmod(numeric) → [quotient, modulus]
num % divisor → remainder
num.remainder(numeric) → remainder
a b a % b (modulo) a.remainder(b) a.divmod(b)
17 5 2 2 [3, 2]
17 -5 -3 2 [-4, -3]
-17 5 3 -2 [-4, 3]
-17 -5 -2 -2 [3, -2]
string.to_i
converts string to integer
expressions and return
an expression always returns a value in Ruby, even if that value is nil or an error
Variable Scope
Defined by where the variable is initialized or created. In Ruby, variable scope is defined by a method definition or by a block.
Inner scope can access variables initialized in an outer scope, but not vice versa.
When is a {} or do end piece of code not create a new scope?
When that code is part of the Ruby language, rather than a method.
Example: for I in arr {something} is not changing scope.
5.times { |x| something } is a method (times), and thus changes scope.
Variable Types
Constants All letters capitalized. Can be changed, but Ruby will warn at run time. Scope is global Global $var. Are available throughout app, and override all scope boundaries. Not typically used in Ruby. Class @@var. Available in a class and each instances. Not created within the class instance, but in the class. Instance @var. Available in current instance of parent class. Some can cross scope boundaries. Local Most common variable, lower case with _ separating words, not starting with symbol.
method variables
Variables do not enter a method unless passed in, and do not leave unless returned.
p
print
puts
print sends output with no additional spaces or line returns
puts sends output with a line return at the end
p sends output in the form the programmer wants to see it, e.g. with brackets, quotes etc., to help with validating that the output is in the form expected.
call stack
call stack is the point in time: the methods, blocks, procs, and lambdas currently running
limited to 10,000 stack entries, will return “SystemStackError” if exceeded.
Conditionals keywords and operators
If, else, elsif, end, and unless keywords
, <=, >=, ==, !=, &&, ||