Ruby Docs

  • When reading the Official Documentation for Ruby you’ll encounter :: in some of the lists referring to classes and modules. This notation is used to denote what module a class is from, as to not avoid confusion for classes that have the same names but are completely different. For example, ModuleName::ClassName

Personally, I don’t think classes should ever have identical names even if they’re from different modules. But that’s just how Ruby’s poor documentation is like…lots left to be desired.

  • As a documentation convention, methods are listed out with either a :: or a # to indicate two different kinds of publicly accessible methods. Methods denoted by :: are considered class methods, while methods denoted by # are considered instance methods. Their use here in Ruby documentation is completely different from their use in actual code.

Styling in Ruby

  1. Tabs should be set to 2 spaces and the indents should also use spaces.
  2. To comment out a line use #
  3. Like Pythons, initializing a variable, method, or file you should use snake_case to name it.
  4. When declaring constants, use UPPERCASE, and global variables are preceded with a ’$’ sign.
  5. In Ruby there’s do/end blocks and these can be multi-lined but if the entire block fits in one line, you ought to use curly braces { }.
  6. Class names follow another naming convention called TitleCase or PascalCase, it’s similar to camelCase but even the first word is capitalized.

Interactive Ruby Shell

The IRB is a shell (program) that works like many other shells and is a great tool for experimenting or writing up something quick to try out a feature. To start the IRB you can simply call ‘irb’ in your terminal/shell. Here’s a quick snippet to clarify.

$ irb
> puts "BOB"
BOB
=> nil
>
  • The first line is us invoking the interactive Ruby shell in our terminal.
  • The second line is Ruby code, since this is IRB, it expects Ruby…
  • The third line is the result of the second line after hitting ‘enter’ to run it, followed by a return value. It’s ‘nil’ due to being unspecified, more on that later on.
  • The last line is also expecting Ruby code to be written.

Loading a file into IRB

To load a file into the Ruby shell simply use the following method: irb -r ./your_file.rb if it is in the same directory.The-r` stands for ‘require’ thus singling to the shell that it needs this file, similar to importing in Python or ‘Require’ in JavaScript.

Ruby Package Manager

Ruby has a mature and well developed community of developers…Code packages can be shared among them using Ruby’s PM, see Package managers called Ruby Gem’s Package Manager. In which every program is organized into a self contained format called a ‘Gem’.