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
, <=, >=, ==, !=, &&, ||
conditional syntax
if conditional action elsif conditional action else conditional action end
if conditional then action end
action if conditional
conditional operators and different data types
<=, , >= are not able to compare different types, e.g. ‘4’ and 4
== and != can compare strings and numbers
comparison operator order of precedence
- <=, , >=
- ==, !=
- &&
- ||
Ternary operator
conditional ? action if true : action if false
case statement syntax
case when action when action else action end
case when condition action when condition action else action end
loop method
Simplest loop
loop do
action
end
-or-
loop { action }
While loops
while condition
action
end
NOT a method, so no separate scope within the loop
Until Loop
Until condition
action
end
NOT a method, so no new scope created within the loop.
do / while loops
loop do action if condition break end end
-eg-
loop do puts "Do you want to do that again?" answer = gets.chomp if answer != 'Y' break end end
for loops
No new scope when running
for x in y do
action
end
-e.g.-
x = [1, 2, 3, 4, 5]
for i in x.reverse do
puts i
end
puts “Done!”
.each method
iterates over each element in a
=~
Pattern matching or regex operator
use: “basketball” +~ /b/ will return 0 because the first b is in the 0 position