DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Demystifying SPF Record Limitations
  • Front-End: Cache Strategies You Should Know
  • Exploring the Capabilities of eBPF
  • Payments Architecture - An Introduction

Trending

  • Demystifying SPF Record Limitations
  • Front-End: Cache Strategies You Should Know
  • Exploring the Capabilities of eBPF
  • Payments Architecture - An Introduction

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 in a more Ruby Way, which will be much better (hopefully!) than the previous solution

Alexandre Gama user avatar by
Alexandre Gama
·
Jul. 07, 20 · Tutorial
Like (1)
Save
Tweet
Share
2.60K Views

Join the DZone community and get the full member experience.

Join For Free

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
 




x
19


 
1
def fibonacci(count)
2
  n1 = 0
3
  n2 = 1
4
  sequence = [n1, n2]
5
  while count > 2
6
        # sum of the previous 2 numbers
7
    n3 = n1 + n2
8
    sequence.push(n3)
9

          
10
      # assigning the new numbers to calculate the next number in the sequence
11
    n1 = n2
12
    n2 = n3
13
    count = count - 1
14
  end
15
  return sequence
16
end
17

          
18
puts fibonacci(8)
19
# 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
 




xxxxxxxxxx
1


 
1
(0..10).each do |number|
2
end



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

Ruby
 




xxxxxxxxxx
1


 
1
(0..count).each do |number|
2
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
 




xxxxxxxxxx
1


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



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

Ruby
 




xxxxxxxxxx
1


 
1
def fibonacci(count)
2
  sequence = []  
3
  (0..count).each do |number|
4
    sequence << number if number <= 1
5
    sequence << sequence[-1] + sequence[-2] if sequence.length >= 2
6
  end
7
  sequence
8
end



Fantastic! Ruby deals with the problem really well!


The next coding challenge is solving the same problem recursively, which is usually harder to think about. However, it has an even better syntax. See you there!

Don’t forget to come by and say hi!

Courses Twitter Youtube Instagram Linkedin GitHub

Coding (social sciences)

Published at DZone with permission of Alexandre Gama. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Demystifying SPF Record Limitations
  • Front-End: Cache Strategies You Should Know
  • Exploring the Capabilities of eBPF
  • Payments Architecture - An Introduction

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: