How to save the default editor in .gemrc so that I can skip providing the editor with the following command:
gem open GEMNAME [-e EDITOR] [options]
1) Using a .gemrc file:
GEM ENVIRONMENT
...
...
The RubyGems environment can be controlled through command line
arguments, gemrc files, environment variables and built-in defaults.
Command line argument defaults and some RubyGems defaults can be set
in a ~/.gemrc file for individual users and a gemrc in the SYSTEM
CONFIGURATION DIRECTORY for all users. These files are YAML files with
the following YAML keys:
:sources: A YAML array of remote gem repositories to install gems from
:verbose: Verbosity of the gem command. false, true, and :really are the
levels
:update_sources: Enable/disable automatic updating of repository metadata
:backtrace: Print backtrace when RubyGems encounters an error
:gempath: The paths in which to look for gems
:disable_default_gem_server: Force specification of gem server host on push
<gem_command>: A string containing arguments for the specified gem command
http://guides.rubygems.org/command-reference/#gem-environment
Note the last line. For example, if I put the following in ~/.gemrc:
open: -e /Users/7stud/Downloads/macvim-snapshot-74/src/MacVim/mvim
then the command:
$ gem open nokogiri
will open nokogiri in macvim on my system.
2) Using environment variables(per mash's comment):
GEM OPEN
...
...
The open command opens gem in editor and changes current path
to gem's source directory. Editor can be specified with -e option,
otherwise rubygems will look for editor in $EDITOR, $VISUAL and
$GEM_EDITOR variables.
http://guides.rubygems.org/command-reference/
That means if I put something like the following in .bash_profile (or .bashrc):
export GEM_EDITOR="/Users/7stud/Downloads/macvim-snapshot-74/src/MacVim/mvim"
then the command:
$ gem open nokogiri
will open nokogiri in macvim on my system. Don't forget to open a new Terminal window or issue the command:
$ source .bash_profile
to load the changes you made to the environment variables in .bash_profile.
Related
I want to use Ruby CGI scripts on an Uberspace 7, but ran into several issues with permissions and security settings, in particular when using gems. How do I install CGI scripts with custom gems?
First, note that Uberspace 7 runs on SELinux. This means that CGI script files in ~/html/ not only have to be executable but also need the correct SELinux context. In this case, the type must be httpd_sys_content_t.
You can view the SELinux context with ls -lZ:
$ ls -Z file1
-rw-rw-r-- user1 group1 unconfined_u:object_r:user_home_t:s0 file1
If some files have the wrong context, the context can be restored with the restorecon command, e.g. restorecon -R ~/html/.
The user installation directory for Ruby gems is ~/.gem/. On Uberspace, gem install installs into that directory by default:
$ cat /etc/gemrc
gem: --no-document --user-install
As the home directory cannot be accessed by the apache process, gems installed there cannot be executed from CGI scripts. You can install gems in /var/www/virtual/$USER/gem instead, create the directory with
$ mkdir /var/www/virtual/$USER/gem
You cannot use the --install-dir parameter for gem install directly as this conflicts with the default parameters mentioned above:
$ gem install mygem --install-dir /var/www/virtual/$USER/gem
ERROR: Use --install-dir or --user-install but not both
Instead, create ~/.gemrc with the following content to override the default parameters (replace <USERNAME> with your actual user name):
gem: --install-dir /var/www/virtual/<USERNAME>/gem
Now the installation of gems should work:
$ gem install mygem
To use the gems in CGI scripts, set the Gem.paths variable before requiring gems:
#!/usr/bin/ruby
Gem.paths = { 'GEM_PATH' => '/var/www/virtual/<USERNAME>/gem' }
require 'mygem'
(... rest of the script)
This is needed as we cannot modify the environment variables (i.e. set GEM_PATH) for the apache process.
How can I use RVM to set a default ruby version for a certain directory? So that every time I cd into that directory, it switches to my preferred version of Ruby.
Directly from the RVM documentation:
RVM supports multiple files allowing to configure a project for automated ruby switching. In any case make sure to add those files to your version control systems as it is part of the project configuration.
Listed in order of precedence:
.rvmrc - shell script allowing full customization of the environment,
.versions.conf - key=value configuration file
.ruby-version - single line ruby-version only
Gemfile" - comment: #ruby=1.9.3 and directive: ruby "1.9.3"
One way is to use a Gemfile and set the ruby version in it. like so:
ruby '2.2.0'
then when you enter the directory you will see the following message from rvm
RVM used your Gemfile for selecting Ruby, it is all fine - Heroku does that too,
you can ignore these warnings with 'rvm rvmrc warning ignore /Users/danmanstx/rails_projects/app/Gemfile'.
To ignore the warning for all files run 'rvm rvmrc warning ignore allGemfiles'.
Create a .ruby-version file in that directory with your version information. To set version as 2.1.2 for a directory, create the file with only "2.1.2" as the content.
$ cat .ruby-version
2.1.2
in rails 6 you only have to change what's inside of .ruby-version in your app folder
Or simply use rvm --ruby-version ruby_version#gemset --create , --create here will create the gemset if it is not present already. If you don't need to specify a gemset, but instead use the default gemset. leave out #gemset --create. ruby_version for example 2.0.0.
I get gems to work fine on my local machine, but on Cloud9 I keep getting `require': cannot load such file. I've had this problem with multiple gems and I don't know what I'm doing wrong.
I have installed the gem, added it to my gem file and did bundle install. It shows in my gem list.
Here is the path for it from bundle show
/usr/local/rvm/gems/ruby-2.1.5#rails4/gems/test_linker-1.0.1
Here is my environment path
GEM PATHS:
- /usr/local/rvm/gems/ruby-2.1.5#rails4
- /usr/local/rvm/gems/ruby-2.1.5#global
I had the same issue. The ENV variables get out of sync between the terminal and the Runner. If you run
exec 'env'
from a ruby script, it may show different GEM_PATH and GEM_HOME than you'll see from the terminal.
To sync them, I had to remove references to #rails4 from env in the ~/.profile file. (just set them in .profile to exactly what they are in the Runner.)
GEM_PATH=/usr/local/rvm/gems/ruby-2.1.4:/usr/local/rvm/gems/ruby-2.1.4#global
GEM_HOME=/usr/local/rvm/gems/ruby-2.1.4
This Runner runs ruby scripts without Rails. If you want Rails you can create a Run Configuration based on the Ruby on Rails Runner.
It would be nice if you could change the ENV variables in the individual Runners. There is an ENV button at the top right, but trying to override GEM_HOME and path there has no effect.
It is always frustrating to install a gem and wait 2 seconds for the gem to install and then wait 30 seconds for the docs, which I never use(Google, anyone?). Why do we force this convention upon ourselves when the local docs normally aren't even beneficial?
I know you can use gem install rails --no-ri --no-rdoc to skip that step but is there a way to simply skip the docs by default?
Add the flags to your ~/.gemrc file.
From the docs:
gem looks for a configuration file .gemrc in your home directory,
although you can specify another file on the command-line if you wish
(with the --config-file modifier). Only one configuration file will be
processed: the rightmost one on the command-line, or the default
$HOME/.gemrc, or none at all.
There are three things you can specify in the configuration file:
command-line arguments to be used every time gem runs
command-line options for "RDoc" (used when generating documentation)
GEMPATH settings
The config file itself is in "YAML" format. Here is an example:
gem: --local --gen-rdoc --run-tests
rdoc: --inline-source --line-numbers
gempath:
- /usr/local/rubygems
- /home/gavin/.rubygems
The effects of such a config file would be:
gem only runs "local" operations (unless you specify --remote or --both on the command-line)
gem generates RDocs and runs unit tests every time it installs something (good idea!)
when it generates RDocs, the given arguments will be used
/usr/local/rubygems and /home/gavin/rubygems will be used as your $GEM_PATH setting
I'm trying to set up a Sinatra app on my web host. I don't have sudo rights to install gems in the system-wide path, which is several subfolders beneath /usr/local, but I do have a gems folder in my app's directory.
Background
This reference gives the following definitions:
GEM_HOME - "Directory containing the master gem repository."
GEM_PATH - "Path list of directories containing gem repositories to be searched in addition to the GEM_HOME directory. The list should be delimited by the appropriate path separator (e.g. ‘:’ on Unix and ‘;’ on Windows)"
Initial settings on login
When I first ssh into this web host, echo $GEM_HOME and echo $GEM_PATH both produce an empty string, but gem list shows several gems.
Trying to change gem location
From the command line, I have set GEM_HOME like this:
GEM_HOME=$PWD/gems # 'gems' folder under present working directory
echo $GEM_HOME # correctly outputs the gem folder I specified
ls $GEM_HOME # shows gems folder contents, namely:
# bin/ cache/ docs/ gems/ specifications/
I also set GEM_PATH to the same folder.
After doing this, gem list still shows global gems rather than the gems in the specified folder, and gem install still tries to install to the global location.
What am I missing?
Use 'export'
Looks like export, as Tass showed, was the missing piece: it makes my local GEM_HOME variable a global one.
Here's what I've done:
export GEM_HOME=$PWD/gems # where to install and look for gems
export PATH=$PWD/gems/bin:$PATH # append the gems' binaries folder to
# the current path, so that I can call
# `bundle install` and have it find
# myapp/gems/bin/bundle
There is no manpage for gem, which doesn't make it easier. I assume GEM_PATH is where to look for the gems, and GEM_HOME is where to install them. Try
export GEM_HOME = "$GEM_PATH"
You could use Bundler as well. Bundler makes it very easy to manage Gem versions, even when sudo access is not possible. You create a file called Gemfile in the root of your application and put lines such as these:
gem "sinatra"
gem "some_other_gem_dependency"
gem "and_so_on_and_so_forth", ">= 1.0"
And then run bundle install --path /where/you/want/your/gems/stored which will install the gems to a path you have access to. You then put this in your config.ru:
require 'rubygems'
require 'bundler'
Bundler.require
require './your_app'
run YourApp
Check out http://gembundler.com/sinatra.html for more info.