DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

1

Running Real Ruby Programs Notes

Ruby programs must be saved with .rb

Run ruby programs from terminal by typing ruby program_name.rb

Use up and down arrows to select command history and tab for autocomplete

Use the require keyword to execute the contents of another file. The argument to require is the relative path in double quotes.

A Gem is a library that someone shared on rubygems.org

activesupport is a gem that extends the built-in Ruby classes like Integer and String with handy methods like ordinalize and pluralize

Bash instal command:
gem install activesupport

Include at the top of the program:
require "active_support/all"

Bundler:
Create a file in your project called Gemfile (no extension)

# /Gemfile

source "https://rubygems.org"

gem "activesupport"
gem "awesome_print"
gem "pry-byebug"
Enter fullscreen mode Exit fullscreen mode

Now, I can install all of the gems at once with one command:
bundle install

Get string:
gets
Pause the program and wait for the user to type something in the terminal and press return. The return value of the gets method will be a String containing what the user typed, which we can store in a variable and then process.

Interactive Ruby (IRB):
IRB is an example of a Read-Evaluate-Print-Loop (REPL)
Run at command prompt:
irb
Exit with exit

Ctrl+C to reset to a clean prompt.

Byebug:
When you launch irb, it doesn’t by default have any connection to any existing Ruby file.

Type byebug to create a breakpoint in the program

The (byebug) is a prompt. This is like an irb session, but at the moment in time where the byebug appeared in the code.
You can continue to have it keep going with the program (until the next breakpoint).
You can also step to move the breakpoint forward by one line.

Ruby Reference

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
heratyian profile image
Ian β€’

Great tips πŸ‘

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post