Day 14 of 30 Ruby Coding Challenge - Fibonacci Sequence the Ruby Way

Day 14 of 30 Ruby Coding Challenge - Fibonacci Sequence the Ruby Way

Day 14 of 30. We're going to solve the famous Fibonacci sequence by taking advantage of features that Ruby offers.

Day 14 of 30. We're going to solve the famous Fibonacci sequence in a more Ruby Way, which will be much better (hopefully!) than the previous solution

Hey friends!

This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series

Fibonacci Sequence

It’s time to organize the kitchen and to get a better code design to solve the Fibonacci Sequence, which was the previous coding challenge:

We want to calculate the first N numbers in a Fibonacci sequence

This was the last coding solution:

Ruby

def fibonacci(count)

 n1 = 0

  n2 = 1

 sequence = [n1, n2]

  while count > 2

        # sum of the previous 2 numbers

    n3 = n1 + n2

   sequence.push(n3)

      # assigning the new numbers to calculate the next number in the sequence

   n1 = n2

    n2 = n3

   count = count - 1

  end

 return sequence

end

puts fibonacci(8)

# 0 1 1 2 3 5 8 13
```

You can be honest, it's not that great.

## The Ruby Way to Solve the Fibonacci Problem

### **Step 1**

Ruby allows us to go from one number to another in a sequence like this:

Ruby

```
(0..10).each do |number|

end


In our example we want to avoid the count mutation (fancy name for change). We can do that by the following code:

Ruby

(0..count).each do |number|

end
```

That’s great because Ruby will **automatically iterate over the array**

### **Step 2**

A better way to store the number in the sequence would be:

Ruby

```
sequence << number if number <= 1

sequence << sequence[-1] + sequence[-2] if sequence.length >= 2


The complete code, a little bit leaner with a better strategy, would be:

Ruby

def fibonacci(count)

  sequence = []  

(0..count).each do |number|

    sequence << number if number <= 1

   sequence << sequence[-1] + sequence[-2] if sequence.length >= 2

  end

 sequence

`````` end

```

Fantastic! Ruby deals with the problem really well!

ruby programming coding ruby on rails algorithm fibonacci tutorial for beginners algorithm analysis coding basics coding challenges

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Day 17 Ruby Coding Challenge - Sum Even Numbers in Fibonacci Sequence - DZone Web Dev

Day 17 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence with a better code design.

Day 16 of 30 Ruby Coding Challenge - Sum Even Numbers

Day 16 of 30 - Ruby Coding Challenges in 30 Days. We're going to sum all the even numbers in a given Fibonacci sequence.This is the blog post version of the Youtube video from the 30 Ruby Coding Challenges in 30 Days series

Day 15 of 30 Ruby Coding Challenge - Fibonacci Sequence Recursively

We're going to solve the famous Fibonacci sequence recursively in Ruby. This is not a better strategy than the previous one, this will be another option. Today, we’ll solve the previous Fibonacci problem using the dreaded recursion approach. A little bit of theory was explored in this video, and today, I’ll straight to the point

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

Day 18 of 30 Ruby Coding Challenge - Finding the Missing Number Game

We're going to play a game: find the missing number in a given array. This first solution will be not that great but we'll get the job done!