Ruby Basics Flashcards
(140 cards)
An edge case is…
something outside of the expected normal input
Give an example of string interpolation
name = “tina”
puts “my name is #{name}”
=>my name is tina
How do we get a hash value?
furniture[“name”]
=>”bed”
Most straight forward way of creating a hash
furniture = {“name” => “bed”, “colour” => “brown”, “material” => “wood”}
How do we add or change data in a hash?
to add new key and value:
furniture[“size”] = “king size”
to change value is same approach:
furniture[“size”] = “double”
How do we define a method in ruby?
def method() end
what does ‘gets’ do?
get string waits for user to make a keyboard input and passes it to the program
what does .chomp do?
removes the carriage return characters at the end of a string in such cases as after using ‘gets’
Constants are declared by…
Capitalizing every letter
Global variables are declared using which symbol?
$variable #usually avoided in ruby as can cause problems
Class variables are declared using which symbols?
@@class_variable
Class variables must be initialized at…
Class level, outside of method definitions
Instance variables are declared using which symbol?
@instance_variable
How do we type ‘else if’ in Ruby?
elsif
Opposite of if?
unless
Short hand for if else
? :
Loop takes a block denoted by..
{ } or do….end and loops it.
Name 2 ways of ending a loop
Break and Return
To skip an iteration in a loop we can use..
next
The opposite of a while loop is an
until loop
what does range do?
captures a range of elements i.e
1..5
=> 1,2,3,4,5
Methods that loop over a given set of data, allowing you to operate within each element in the collection
Iterators
Lines of code that are ready to be executed, contained within { } or do and end are called
Blocks
Recursion is the act of…
calling a method from within itself