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"
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.
Top comments (1)
Great tips π