Ruby Basics Flashcards
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
.first
finds the first element from an array
.last
finds the last element in an array
.pop
removes the last element from an array and returns it
(this method mutates the caller)`
what is the shortcut for the push method?
array«
also known as shovel
.push
adds element to the end of an array
mutates the caller
.map and .collect
iterate over a collection applying a block to each element in the collection, then returning a new collection.
.delete_at
takes an array index and deletes the corresponding array item without leaving a gap
.delete
takes a value and deletes the corresponding array item(s)
.uniq
finds duplicates within an array and returns a new array with duplicates removed
.select
returns all elements in a collection that return true to the conditions that you specify
What does the bang operator (!) after a method usually signify?
that it will mutate the caller
.unshift
adds a specified value to the start of an array
.include?
checks if an argument given is included in the array
? after a method usually indicates?
that it will return a boolean value.
.flatten
converts a nested array into a normal array
.eachindex
passes the index of an array element rather than a value into the block
.each_with_index
passes both index and value of array element to the block
.sort
returns a sorted array
What is the difference between .each and .map?`
each discards the return value making no changes whereas map creates a new array from its return value
What is a class?
Basically a custom data type that you can create
What is the setter/getter syntax for classes?
attr-accessor
Putting a : before anything defines it as what?
Symbol
Multi line comment
=begin
=end
What’s the difference between anIntegerand aFloat?
Integer is a whole number, float has a decimal
What’s the difference between==, and===?
== is a comparison operator, whereas === typically asks whether the thing on the right is of the same type or is a member of the thing on the left.
How do you do exponents in Ruby?
x**y (x to the power of y)
What’s the difference between(1..3)and(1…3)?
(1..3) includes 1 and 3 whereas (1…3) excludes 3
What are three ways to create a range?
1..10
1…11
Range.new(start, finish)
What’s the difference between single and double quotes?
Nothing apart from you should only use string interpolation inside double quotes and if you need to use quotes within quotes then use single inside doubles.
What are escape characters?
Escape characters () waiver characters and meta characters from acting how they would normally, escaping their function I.e “my name is #{name}” => “my name is #{name}”
What are line breaks?
code used to create a new line in the terminal i.e (\n)
How do you make other things into strings?
to_s