Getting to know Ruby Flashcards
Wat would be an example of a UNLESS control statement?
unless x == true
do this
end
What would be a WHILE control
x = 10
while x > 9
puts x
x -= 1
end
What would be an example of an UNTILL statement?
x = 10
untill x == 5
puts “x is true “
x -= 5
end
besides ittarations with .each or .times, what flow method is often used in other languages?
The for loop!
Although ruby has it, there are smarter alternatives.
for x in 1..10
puts x
end
Describe a CASE statement, as if it was an IF statement.
age 23
case
when age >= 23
puts “drink”
when age <= 23
puts “dont drink!”
else
puts “think ab out it.. there is no else..”
end
A CASE statement, that internaly uses ‘===’
‘if this then that’
That is, the string litural comparrison or a superset equal comparrison. And littuarly compares trough conversion.
name = “Fisher”
case name
when /fish/i then puts “Somethings fishy”
when ‘Smith’ then puts “Your name is smith”
end
The IF else Statement is declares as follows:
if x == true
p “x is true”
elseif y == true
p “ y is true “
else
p “everything else is true”
end
Shorthand oneliner for 2 variables?
puts “good!” if a == true and b == true
a shorthand oneliner while loop?
times_2 = 2
times_2 *= 2 while times_2 < 100
What is the difference between a function, and a method?
What do they have in common?
Function is defined outside of a class.
Method is defined inside of a class.
They’ll both always belong to a class.
Describe characteristics of a Method/Function.
Ex: How is it written?
- parantheses are optional
- no need for type declaration
- can return whateffer you want
- return is optional
- last evaluation will be returnd
Describe a Predictive Method.
A Prediective Method is declared by appending the “?”, and will evaluate upon what it is called to either true or false.
It can take arguments.
def is_a_number ?(number)
return false if !number.not_a?(number)
true
end
puts is_a_number? 3
true
How can you implement a Default Value for a method argument?
def method_with_default( n = 5 )
n % 5
end
puts method_with_default(4)
=\> 20 puts method\_with\_default #=\> 25
What is the use of the Splat operator?
How would it be implemented in a Method Argument?
- Splat operator takes any number of arguments and stores them into an array.
- the splat operator can also be a ‘middle’ argument, as shown below.
def method_with_arguments( first, *middle, last)
puts middle.max
end
puts method_with_arguments(“Hi”, 4, 432, 2, “bye”)
=> ‘432’
What is a ‘block’ ? And what is it’s use?
- Blocks are chunks of code, that are being executed by a Method or Function.
- They are often used as itterators
- can be declared with oneliner: { puts “this” }
- can be declared with do - end
do
puts “this
end - can Implicit or Explicit
How would you call a Block, that prints “Hi” 5 times?
5.times { puts “Hi” }
or
- times do | greet |
greet. puts “hi”
end
Write an Implicit block,
- that yields twice
- if block_given? is True
- else returns “No block”
def two_times_implicit
return “No block” unless block_given?
yield
yield
end
Write an Explicit block
- calls twice if block is given
- or returns “No block” if not
def explicit_block(&this_block)
return “No block” if this_block.nill?
this_block.call
this_block.call
end
What is between the pipe ( | paramater | ) characters if you declare a block?
How about the block parameter?