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?

Write a method reading ‘text.txt’, that puts it out.
Try escaping line characters.
File.read(‘text.txt’) do | line |
puts line.chomp
end

Write a method to write to the ‘text.txt’ file.
- Any line will do.
- what extra attention do you have to be aware of?
you’ll have to give the Class.method (File.open) the argument to “w” short for “write” to the file..
File.open(“text.txt”, “w”) do | line |
line.puts “Hi there”
end

Open the file ‘text.txt’ if it exists.
Write a Predictive method to do so, and put out the lines.
if File.exist?(‘text.txt’)
File.read(‘text.txt’) do | line |
puts line.chomp
end
end

How would you handle an error..
if for example a file doesnt excist?
best practise is to use a cycle of
begin - rescue - end.
the ‘rescue’ catches the Eceptions witch then can be assigned to a variable.
begin
File.foreach(‘not-exist’) do | line |
puts “doesnt matter..”
end
rescue Exception => e
puts e.message
puts “lets pretend that diddn’t happend”
end
end
end

How would you puts your enviroment varaible stating what editor you use?
puts ENV[“editor”]
=> “Sublime text 3 offcourse”




















