Book Image

Comprehensive Ruby Programming

By : Jordan Hudgens
Book Image

Comprehensive Ruby Programming

By: Jordan Hudgens

Overview of this book

Ruby is a powerful, general-purpose programming language that can be applied to any task. Whether you are an experienced developer who wants to learn a new language or you are new to programming, this book is your comprehensive Ruby coding guide. Starting with the foundational principles, such as syntax, and scaling up to advanced topics such as big data analysis, this book will give you all of the tools you need to be a professional Ruby developer. A few of the key topics are: object-oriented programming, built-in Ruby methods, core programming skills, and an introduction to the Ruby on Rails and Sinatra web frameworks. You will also build 10 practical Ruby programs. Created by an experienced Ruby developer, this book has been written to ensure it focuses on the skills you will need to be a professional Ruby developer. After you have read this book, you will be ready to start building real-world Ruby projects.
Table of Contents (20 chapters)

How to code a Fibonacci digit counter

In this section, we are going to solve another fun math problem that asks us to solve this problem: what is the index of the first term in the Fibonacci sequence to contain 1,000 digits?

In case your college algebra is a little rusty, the Fibonacci sequence is a series where you add the next number to the previous number in that series. These numbers can get massive quickly because the addition to the previous value creates a mathematical snowball effect.

Though the problem sounds daunting, it can be solved easily in Ruby.

We are going to start by creating a method called fibonacci_digit_counter and define some variables:

def fibonacci_digit_counter
num1, num2, i = -1, 0, 1
end

Next, we will create a while loop inside of the fibonacci_digit_counter method and iterate over the digit value of i:

while i.to_s.length < 1000
num1 += 1
i,...