Control flow techniques

by msypniewski511 in Ruby for Rails

Conditional execution

Conditional Statements: Conditional statements, such as if-else and switch-case, allow programmers to execute different blocks of code based on certain conditions.

If/elsif unless

if/elsif/elese

if condition
  # code here, executed if condition evaluates to true
end
if x > 10 then puts x end
if x > 10; puts x; end
if x > 10: puts x; end
if condition
  # code executed if condition is true
else
  # code executed if condition is false
end

------------------------------------------

if condition
  # code executed if condition1 is true
elsif condition2
  # code executed if condition1 is false
  # and condition2 is true
elsif condition3
  # code executed if neither conditions
  # nor condition2 is true, but condition3 is
end

You can have any number of elsif clauses in a given if statement. The code seg-
ment corresponding to the first successful if or elsif is executed, and the rest of
the statement is ignored:

print "Enter an integer: "
n = gets.to_i
if n > 0
  puts "Your number is positive."
elsif n < 0
  puts "Your number is negative."
else
  puts "Your number is zero."
end

unless

if not (x == 1)
if !(x == 1)
unless x == 1

Conditional modifiers

puts "Big number!" if x > 100
puts "Big number!" unless x <= 100

Case statements

Case statement operate on ===

print "Exit the program? (yes or no): "
answer = gets.chomp
case answer
when "yes", "y"
  puts "Good-bye!"
exit
when "no"
  puts "OK, we'll continue"
else
  puts "That's an unknown answer -- assuming you meant 'no'"
end

Loops and looping technique

loop method

loop { puts "Looping forever!" }
loop do puts "Looping forever!" end

Controlling the loop

n = 1
loop do
  n = n + 1
  break if n > 9
end

------------------------------------

n = 1
loop do
  n = n + 1
  next unless n == 10
  break
end

while/until

while

n = 1
while n < 11
  puts n
  n = n + 1
end
puts "Done!"

-------------------------------------------

n = 1
begin
  puts n
  n = n + 1
end while n < 11
puts "Done!"

---------------------------------------------
n = 10
while n < 10
  puts n
end

!=

n = 10
begin
  puts n
end while n < 10

until

n = 1
until n > 10
  puts n
n = n + 1
end

while and until as modifiers

n = 1
n = n + 1 until n == 10
puts "We've reached 10!"

Looping based on a list of values

for in

celsius = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
puts "Celsius\tFahrenheit"
for c in celsius
  puts "c\t#{Temperature.c2f(c)}"
end

Iterators

Exceptions and error handling

Code blocks, iterators, and the yield keyword

Ruby Blocks Explained | Deep Dive with Examples
Ruby map(&:method) syntax - meaning & usage
The basics of yielding to block
When you call a method—any method, any time, with or without arguments you have the option of supplying a code block.

object.method_name {
  # code inside block
}

object.method_name do
  # code inside block
end

The yield keyword in action

def greet
  puts "Hello!"
  yield if block_given?  # Execute the block if provided
  puts "Goodbye!"
end

 # Calling the method with a block
greet do
  puts "How are you?"
end
Hello!
How are you?
Goodbye!

Passing arguments to a code block

 # in method definition:
def meth(a,b,c)
 # code block:
some_method {|a,b,c|
  # code here
}

def greet(name)
  puts "Hello, #{name}!"
  yield(name) if block_given?  # Pass the 'name' argument to the block
  puts "Goodbye, #{name}!"
end

 # Calling the method with a block that expects an argument
greet("Alice") do |name|
  puts "How are you, #{name}?"
end
Hello, Alice!
How are you, Alice?
Goodbye, Alice!

Returning a value from a code block

def more_yielding
  puts "The code block shall multiply a number by 10."
  result = yield(3) if bloc_given?
  puts "The result of this operation is #{result}."
end
more_yielding {|x| x * 10 }
The code block shall multiplay by 10.
The result of this operation is 30

Performing multiple iterations
The process of yielding from a method to a block is called iteration, and any
method that yields to a block is called an iterator.

def temp_chart(temps)
   for temp in temps
     converted = yield(temp)
     puts "#{temp}\t#{converted}"
   end
end
celciuses = [0,10,20,30,40,50,60,70,80,90,100]
 temp_chart(celciuses){|cel| cel * 9 / 5 + 32 }
0   32
10  50
20  68
30  86
40  104
50  122
60  140
70  158
80  176
90  194
100 212
def greet(names)
  puts "Hello, everyone!"
  names.each do |name|
    yield(name) if block_given?  # Yield for each name
  end
  puts "Goodbye, everyone!"
end

 # Calling the method with a block
greet(["Alice", "Bob", "Charlie"]) do |name|
  puts "How are you, #{name}?"
end
Hello, everyone!
How are you, Alice?
How are you, Bob?
How are you, Charlie?
Goodbye, everyone!

Using different code blocks

fahrens = [32,62,92,122,152,182,212]
temp_chart(fahrens) {|fahr| (fahr - 32) * 5 / 9 }
32  0
62  16
92  33
122 50
152 66
182 83
212 100

More about for

for x in array of values
  code
end
for x in [1,2,3,4,5]
  puts x * 10
end
=
[1,2,3,4,5].each {|x| puts x * 10 }
people = [Person.new("Maciej"), Person.new("Sypniewski")]
names = people.map {|p| p.name}
names = people.map(&:name)

0 Replies


Leave a replay

To replay you need to login. Don't have an account? Sign up for one.