Some Ruby tools we've covered:
- RubyGems
- Rubocop
- The Command Line
- Code Editors
- Git & Github
Some Ruby tools we'll look at in this book:
- Rbenv / RVM
- Gems
- Rake
- Bundler
And Ruby tools for later:
- SQL
- Rails
- Databases
It's a lot, but it's also what makes Ruby so handy. This book is about learning how to navigate through your coding environment and debug problems. These are not univeral coding concepts that require mastery.
- Most systems come with Ruby pre-installed.
- Did you know: Homebrew is a package manager.
How did Ruby get on my system?
It uses pre-built work-spaces, based on standard Linux distributions. It automatically installs the RVM Ruby Version Manager. RVM allows one to manage Ruby components without root privileges, which is safer for the user. Cloud9 is the easiest way to install Ruby.
- Ruby is part of the standard OS X/macOS installation. Find the ruby command at
/usr/bin/ruby. This is your "system ruby"./usr/bin/ruby -vtells you which version you're using. - Undesireably, Mac system Ruby needs root access to install and manipulate other Ruby components. Root access isn't always available to the developer, so this can be a ball-ache.
- So it's outdated and denies you full access, this is why you should install a Ruby Version Manager and use it to install the "Rubies" you need.
- You can do this by installing homebrew, which is a package manager designed for Macs.
- If Ruby is not already installed you can install it with your package manager (RPM, Yum, DPKG) and it will end up in the
/usr/binand/usr/lib/rubydirectories.
-
which rubytells you where ruby is. Mine is/usr/bin/ruby, which is the pre-installed Mac version, and not good enough. -
If
which rubyhas rbenv in it, you are using a version installed by rbenv. Ditto for RVM. -
I need to install a Ruby Version Manager later (rbenv).
-
The system Ruby also stores additional commands, like
irbandrakein the same directory. -
usr/bin/rubycontains other Ruby libraries and gems, but actually it's an alias for the real directories, nestled more deeply in/System/Library/Frameworks, which is a place you should never modify directly. -
other responses to
which rubyare:/usr/local/rbenv/shims/ruby** NB: 'rbenv' or 'shims' indicates you're using an rbenv-managed ruby./usr/local/rvm/rubies/ruby-2.2.2/bin/ruby
-
ruby -vtells you which version of ruby you have installed. -
The latest version of Ruby can be seen here
- The core library
- The standard library
- The
irbREPL - The rake utility (a tool to automate Ruby dev tasks)
- The gem command (a tool to manage Ruby Gems)
- Documentation tools (
rdocandri)
- Packages of code that you can download, install and use in your Ruby program or from the command line.
- The
gemcommand manages your gems. - A list of gem commands can be found here
- There are thousands of gems. Here are some we have already encountered:
rubocopprysequel? noperails? not yet!
- You can find gems in the ruby gems library or you can search that library with a specialized remote library.
- Install your gem with
gem install gem_name. You can specify additional remote libraries to connect to your gem.
- When
geminstalls a gem it saves its files in your local library. - Where gem creates this local library depends on a few things (whether you are using a system Ruby that needs root access, a user maintainable Ruby, the specific Ruby version number and whether you use a Ruby version manager like RVM or Rbenv.
gemknows where to put things and gets on with it.
- Sometimes you may want to look at a gem's source code. Maybe to understand how the gem works, or diagnose a problem with a gem not working.
- Source code is available online, but if you have no connection you can find the gem's code in your local library where your gem is saved.
- Do not change the source code of your gem. It may cause unexpected behaviours and changes will be lost at the next update.
- You can find where gems are being put by calling
gem env. For me this prints the following:
RubyGems Environment:
- RUBYGEMS VERSION: 3.2.16
- RUBY VERSION: 2.6.3 (2019-04-16 patchlevel 62) [universal.x86_64-darwin20]
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.6.0
- USER INSTALLATION DIRECTORY: /Users/sandyboy/.gem/ruby/2.6.0
- RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby
- GIT EXECUTABLE: /usr/local/bin/git
- EXECUTABLE DIRECTORY: /usr/bin
- SPEC CACHE DIRECTORY: /Users/sandyboy/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Library/Ruby/Site
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-20
- GEM PATHS:
- /Library/Ruby/Gems/2.6.0
- /Users/sandyboy/.gem/ruby/2.6.0
- /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- :gemdir => ["~/.gem/ruby"]
- "install" => "-n /usr/local/bin"
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /Users/sandyboy/.rvm/binRUBY VERSION: This is the version number of Ruby associated with the gem you just ran. Each version of Ruby on your computer has its owngemcommand.RUBY EXECUTABLE: This is the location of the ruby command that you should use with the gems managed by this gem command. This information is often useful whenRUBY VERSIONreveals agem/rubymismatch.INSTALLATION DIRECTORY: This directory is wheregeminstalls gems by default.USER INSTALLATION DIRECTORY: In some circumstances gem may install gems somewhere in your home directory, rather than your system-level directory. This is that alternative location.EXECUTABLE INSTALLATION DIRECTORY: This is where commands that are usable from the terminal are saved.REMOTE SOURCES: This is the remote library used by this gem version.SHELL PATH: This is the value of yourPATHvariable. It tells your shell where to look for the executables. This can be helpful if you have acommand not founderror message. This is relevant to PATH
- To see which version of a gem you are running you need to know the full path name. You can do this with some debugging code (or a
binding.pry) right after the point where you require thegem. This example looks for the gem 'freewill':
puts $LOADED_FEATURES.grep(/freewill\.rb/)
# => /usr/local/rvm/gems/ruby-2.2.2/gems/freewill-1.0.0/lib/freewill.rb
- This command searches
$LOADED_FEATURESarray for entries includingfreewill.rband prints all matches. Then you can see if the version you're using matches the version you require. Then you can install the right version, or the matching version of Ruby.
- You may have multiple versions of a gem, and some programs which require an older version (rather than the default, most recent version. There are a few solutions to this:
- Provide an absolute path name to
require. - Add an appropriate
-Ioption to the Ruby invocation. - Modify
$LOAD_PATHbefore requiring the gem. - Modify the
RUBYLIBenvironment variable.
- Provide an absolute path name to
- But theses solutions are all unmanageable hacks, the real solution is to use Bundler.
- Ruby is an evolving language and sooner or later you will write a program that needs a specific version of Ruby. Ruby version managers can help you to switch between Rubies for different projects.
- Install
rbenvwithbrew install rbenv - A note on 'shell functions'
rvm list rubiesto check if you already have the ruby you need.rvm install 2.2.2to install what you need.
rvm use 2.2.2rvm use 2.3.1 --default$ rvm use defaultto restore default.- It's fairly easy to switch versions, but better to automate it.
- To do this create a
.ruby-versionfile in your project's root directory , containing the version number of ruby you want to use when programming files in that directory. For example:
- To do this create a
cd ~/src/magic
rvm --ruby-version use 2.2.2
- which creates a
.ruby-versionfile in that directory. For good measure you should set your default version in your home directory:
cd
rvm --ruby-version default
- RVM, very cheekily, replaces your
cdcommand with its own shell command. So when you change directories, it keeps track and supplies the right Ruby!
- RVM creates a directory when installed called the RVM path. To see the location of this path run
rvm info rvm
- The most common error is that you make a mistake with which version of Ruby you're using or install/update gems with the wrong
gemcommand. - A note on some troubleshooting tips.
- A note on gemsets, which is a poor substitute for Bundler.
- You may feel that RVM and rbenv work very similarly. There are minor syntax variations. It is under the hood that the real differences are to be found.
- rbenv uses a set of small scripts called 'shims'. They are saved in a place where Ruby must search when it can't find a command, like
rubocop.
- Step 1 of rbenv is installing the ruby you need.
- run
rbenv versionsto see which versions of ruby you already have. - Unlike RVM, rbenv does not provide a way to install rubies by default. But we can make this, by installing the
ruby-buildrbenv plug-in:
brew install ruby-build
- Then just
rbenv install 2.2.2
- set default ruby (here to version 2.3.1) with:
rbenv global 2.3.1 - use the
rbenv localcommand from a project's directory to set a project's ruby. As in:
cd ~/src/magic
rbenv local 2.0.0
- This works just like RVM.
- rbenv creates a directory called the rbenv root directory at installation. Find it with
rbenv root. - The root directory contains a
versionsdirectory and ashimsdirectory. - inside
versionsyou will find one directory for each version of ruby. - Now
gem envshows you the active version of the ruby and gem currently active.
- rbenv is safer than RVM because it makes small, one-time changes to your PATH and leaves your system to run as it was designed to.
- Then a section on how to troubleshoot rbenv.
- Don't forget to check these out as your use of rbenv expands. Of particular note is
ruby-buildwhich adds theinstallcommand to therbenvcommand so you can install rubies directly with rbenv.
- Dealing with dependencies.
- Most devs use a Ruby Version manager to manage multiple rubies, you can use this to deal with gem dependencies, but most don't. For that we have a dependency manager like Bundler.
- My version of ruby has Bundler pre-installed.
- Your gem file is where you save your gems.
- Note that the gem is Bundle, but the command is
bundler(even thoughbundleis an alias). - Bundler won't always choose the latest gem version. It may just pick one which works.
- add
require 'bundler/setup'to your app, before other apps. (Unnessecary if your app is a Rails app). bundler/setupfirst removes all Gem directories from Ruby's$LOAD_PATHglobal array. Normally Ruby would look in this array to find the directories that it searches for required files.bundler/setupsuplants these with itsGemfile.lockand adds those directories to the$LOAD_PATH. So now all you need to do is run your app and the correct version of the gem will be loaded.
- They are where they were before you installed Bundler. No change.
- Something about using
binstubsfeature.
- For when the app has conflict with your
bundler/setupcommand. - A section on when you should use this command. Some say always.
- An alternative to
bundle exec.
eg.
Gem::LoadError: You have already activated ...
or
in `require': cannot load such file -- colorize (LoadError)
- And some more tricks to do when bundle fails. Many of them involved uninstalling and reinstalling Bundler.
- Rake is a rubygem that automates many common tasks involved with building, testing, packaging and installing programs. It comes with Ruby when installed. It automates the following tasks:
- Set up required environment by creating directories and files.
- Set up and initialize databases
- Run tests
- Package your app and all of its files for distribution
- Install the application
- Perform common Git tasks
- Rebuild certain files and directories based on changes to other files and directories.
- So it can automate any task involved in development/ testing/ release cycles.
Rakefile- Rake runs a default task if you do not specify which task you want when calling it.
desctaskrake -T: find out what tasks it can run.bundle exec rakemay be necessary to run the right rake version.
- Because nearly every Ruby project has one.
- You basically always use Rake, because it is so useful. Here are some tasks it can automate:
- Run all tests.
- Increment a version number.
- Create your release notes.
- Make a complete backup of your local repo.
- Sure, you can do all these things manually, but why would you?
- the Pry gem
Rakefile
- This is by no means all the tools. Some coming later in the course are:
- Sinatra
- Sequel
- Rails gem
- PostgreSQL
- How tools interact.
- Ruby Version Manager is at the top level.
- Within each Ruby you can have many gems.
- Each gem can have mutiple versions.
- Homebrew
- a package manager designed for Macs; you may need this to install some programs that are useful with Ruby, like PostgreSQL.
which ruby- determine where your system finds the ruby command.
ruby -v
- To determine what version of Ruby is current
- what is
rake?- a tool to automate Ruby development tasks.