Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Table of contents
Ruby Definition
File Structure in Ruby
A Simple Ruby CLI application
lib/ (app/)
spec/ (test/)
.rspec, .learn, Gemfile, Gemfile.lock, Rakefile
Frequently Asked Questions
What do subdirectory app controllers and app helpers do?
What is Active Record in Ruby?
Where are Rails files stored?
Last Updated: Mar 27, 2024

File Structure in Ruby

Master Python: Predicting weather forecasts
Ashwin Goyal
Product Manager @


In this blog, we will learn about the File Structure in Ruby. Ruby is a lively, open-source programming language emphasising ease of use and productivity. It features an attractive syntax that is simple to read and write. Now we will review how those lexical tokens fit together to form a Ruby program's more enormous syntactic structures. This article describes the Ruby program's file design, beginning with the most straightforward statements and progressing to the most significant modules.

Ruby Definition

Ruby is a flexible programming language with a complicated but expressive grammar and a comprehensive and powerful API in its core class library. Lisp, Smalltalk, and Perl inspire Ruby, but it uses a grammar that is simple to learn for C and JavaTM programmers. Ruby is a procedural and functional programming language that may also be used for object-oriented programming. It has extensive metaprogramming capabilities and can be used to develop DSLs (domain-specific languages).

Ruby is a carefully balanced language. Yukihiro "Matz" Matsumoto, the language's developer, designed a new language that blended functional and imperative programming, he integrated features of his favourite languages (Perl, Smalltalk, Eiffel, Ada, and Lisp).

In a way that parallels life, he has often stated that he is "trying to make Ruby natural, not simple."

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

File Structure in Ruby

Ruby programs do not have to be contained in a single file. Many apps, for example, load additional Ruby code from external libraries. Programs require code from another file. Require examines a search path for specified code modules and prevents any given module from being loaded more than once.

To begin, a "shebang" comment must occur on the first line of a Ruby program to instruct the (Unix-like) operating system on how to execute it.

Second, the comment is ignored if a Ruby program includes a "coding" comment.

If the first line is a shebang, the comment must be placed on the second line.

Third, if a file has a single line with the token __END__ and no whitespace,

The Ruby interpreter will stop processing the file at that point, whether it's before or after.

The rest of the file could include any data that the software can read with the help of the

DATA is an IO stream object. 

As the complexity of our apps grows, we'll need to keep our project files organised. Based on the code's performance, there is a reasonably consistent convention for where to put code in a project. Using a straightforward approach, we'll learn how to organise code in a Ruby application. On this foundation, we'll construct.

A Simple Ruby CLI application

A folder structure similar to this should be visible from the root directory of a Ruby application:

  • bin

              –  tictactoe

  • config

              – environment.rb

  • lib

              – tic_tac_toe.rb

  •  spec

              – tic_tac_toe_spec.rb

   – spec_helper.rb

  • Gemfile    
  • ttt.rb    

You won't have all those directories or files, but you'll have a comparable structure.

Top-level directories (the most important folders in your project) include bin, lib, config, spec, and occasionally app.

You might also notice top-level files like. Learn,rspec, Gemfile, or Rakefile (files directly within your project). In some labs, an actual programme file, such as ttt.rb, may be found at the top level.

We make every effort to inform you of the location of the files you need to view or update in each lab. This is only a general statement.

In the terminal, cd into the directory of a lab you recently solved. Within that directory, type ls -lah to get a human-readable list of all the files in the current directory, including hidden files. Something similar should be seen.

Let's speak about the many types of code that go where.

Each of these aspects of Ruby file structure is demonstrated in the following code:

#!/usr/bin/ruby -w                  shebang comment
# -*- coding: utf-8 -*-             coding comment
require 'socket'                    load networking library
 ...                                program code goes here
__END__                             mark end of code
 ...                                program data goes here


We usually put code related to running our actual programme in the bin/ directory. Our bin executable files, which run CLI applications, are detailed here.

lib/ (app/)

Most of our code is stored in the lib/ or Library directory in typical Ruby programmes and the app/ directory in Rails projects or larger Ruby systems. All of the files that specify what our software can accomplish are contained in this directory. The files in this directory contain all of the methods and classes that our software requires. One file may specify a set of plans for a song by an artist, while another may define a set of methods for searching for a piece by genre. These methods could be combined to create a Music Search application. This directory is where we spend the majority of our time writing code.


A complicated programme may require hundreds of separate files containing source code, method definitions, classes, and other information that make up all of the code needed to operate the programme. This code is referred to as the application's environment. In general, we keep all the code required to set up the atmosphere in config/. This might be found in a config/environment.rb file or the complete config/environments/ directory.

spec/ (test/)

Tests are written by great developers for their code. It's critical to be able to develop tests that ensure your code operates as expected, whether you use Test-Driven Development or not. It's also essential to be able to read and comprehend tests and the criteria they specify for your code. All of our tests are saved in the test/ or spec/ folders.

.rspec, .learn, Gemfile, Gemfile.lock, Rakefile

In most Ruby projects, there is a set of files that provide tooling and support for your programme. Gemfile, which Bundler uses to manage gem dependencies, is an example of such a file. Another option is to use a Rakefile to specify application tasks. For the time being, don't be concerned about these files.

Frequently Asked Questions

What do subdirectory app controllers and app helpers do?

The controller classes are found in the controller's subdirectory by Rails. A user's web request is handled by a controller. App/assistants Any assistance classes needed to assist the model, view, and controller classes are stored under the helper's subfolder.

What is Active Record in Ruby?

The model, or M in MVC, is the layer of the system responsible for providing business data and logic. Active Record is the M in MVC - the model. Active Record makes it easier to create and use business objects whose data needs to be stored in a database for a long time.

Where are Rails files stored?

Active Storage keeps all submitted photos on your local drive in the storage subdirectory of the Rails application directory by default in the development environment. That's the file you put on the server.


In this blog, we have extensively discussed the File structure in Ruby. We hope that this article has helped all of you with additional information about the Ruby programming language. And to learn in-depth about Structure-execution, check out the course on our Ruby on the Coding Ninjas website. You can also consider our Online Coding Courses such as the DSA in PythonC++ DSA CourseDSA in Java Course to give your career an edge over others. Do upvote our blog in helping other ninjas grow.
Delighted Programming!

Next article
Program Execution in Ruby
Live masterclass