Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Patrick Shushereba
Patrick Shushereba
10,911 Points

Best Practices Question

I wanted to know, as a general rule, when I should break my program up into multiple files. I'm trying to write a more comprehensive bank account program in Ruby. I can see that the main class that I have is going to grow in size rather quickly. Would it be best to have each part of the program in its own file and "include" it in the main program? Or is it best to just work off of one major class no matter the size?

1 Answer

Joshua Watson
Joshua Watson
17,373 Points

Most other object oriented programming languages you will create a separate file for each of your classes and you can most definitely do this for Ruby and I would encourage it. You will "require 'path/to/class_file.rb'" within your main script so you can instantiate the class with no problem. It is best to create a gem and bundle all of your classes together. I recommended watching the Ruby Bundler and Gem courses here within Treehouse to better package all of your class files for one project.

Patrick Shushereba
Patrick Shushereba
10,911 Points

Thanks for getting back to me. So my main file would just have whatever code I need for any menus or to call methods in those other files?

Joshua Watson
Joshua Watson
17,373 Points

Correct. You will call your classes/modules methods within your "main" file. You can keep your module/class files anywhere within the gem just as long as you enter this into your main file:

require_relative '../path/to/class_file.rb'

foo = class_file.new()
puts foo

This will display the object foo onto the console that has been created from the class_file class.