Trying Ruby: why can't I install nanoc? - ruby

I try to install nanoc http://nanoc.stoneship.org/docs/2-installation/
by typing in irb
gem install nano
it says undefined variable or method 'nanoc' ?

You need to install it from the shell, not IRB. Gem is a command, i.e.
% which gem
/usr/bin/gem
% gem install nanoc
ian.

That gem install command is meant to be run in your normal system shell (something like Bash, for example).
irb is a Ruby shell, it interactively executes Ruby code. You'll notice that the instructions you link to immediately tell you to quit irb after starting it (they only told you to run it to make sure Ruby was installed).

Related

pry not available in the irb debugger

I want to use pry from within irb/debugger, so I can:
invoke step, next, continue, finish inside of pry
still be able to set breakpoints, etc.
What I did is the following:
$ gem install pry
$ gem install debugger
$ gem install debugger-pry
In the code I have inserted require 'debugger'; debugger
Then I start my program with ruby example, the irb promp starts and as described here it should display the pry command on help, but it does not.
ruby-debug help v1.5.0
...
(rdb:1) pry
*** Unknown command: "pry". Try "help".
Any idea how I could check whether it is installed correctly or what am I missing?
I think you need:
require 'debugger/pry'
you could add it to the top of your example.rb file.

How to use Bundler with path specified Ruby version

I am on a VM (Lucid 64b) with a system Ruby version of 1.9.3p0.
I have a Ruby script that creates a .deb file -- The script needs to use Ruby 1.8.7 which I have installed in /foo/ruby/1.8.7.
There is an existing Gemfile to be used with Bundler
I can't use RVM and I can't install gems at the system level.
My .bashrc includes (and has been sourced)
export PATH=$PATH:/foo/ruby/1.8.7/bin
but ruby -v still gives me
ruby 1.9.3p0 (2011-10-30) [x86_64-linux]
Questions
How can I change the Ruby version for my user to use Ruby 1.8.7?
I've run: bundle install --path vendor/bundle
So in that directory (actually ./vendor/bundle/ruby/1.8/cache/gems) are all the gems I need but, when I run the Ruby script it doesn't find the required gems. I run the script like so /foo/ruby/1.8.7 script_to_gen_deb_file.rb
How can I get ruby to see/use the bundled gems?
Update
I was able to solve it. I needed to use
/foo/ruby1.8.7/bundle exec /foo/ruby1.8.7/ruby script_to_gen_deb_file.rb
I had tried this before, but I got an unrelated error and believed there was an environment problem.
Change your path so the special ruby gets precedence?
export PATH=/foo/ruby/1.8.7/bin:$PATH

starting pry shell

I have downloaded the zip package for windows of pry an alternative for irb shell from github. I have unzipped the package.
How do i start the pry shell ?
You don't need the zip package, Pry is just a regular gem. In a terminal -- or whatever Windows calls those nowadays -- do the following (assuming you don't need something like sudo for Ruby on Windows):
gem install pry pry-doc
Done. Now start it with pry.

How do I run a Ruby Gem?

This might seem stupid, but I recently tried to install SASS and followed their instructions:
$ gem install sass
$ sass --watch [...]
So I followed along:
root#server:~# gem install sass
Successfully installed sass-3.1.15
1 gem installed
Installing ri documentation for sass-3.1.15...
Installing RDoc documentation for sass-3.1.15...
root#server:~# sass
bash: sass: command not found
Despite looking around like an idiot trying to find some simple way to run something like gem run sass or some other workaround to make it function, I am more or less at a loss.
It seems that Debian/Ubuntu drops ruby gems into /var/lib/gems/1.8/bin.
So the solution (at least for Ubuntu/Debian) is:
$ sudo -s
# echo 'PATH=/var/lib/gems/1.8/bin:$PATH' > /etc/profile.d/gemspath.sh
# chmod 0755 /etc/profile.d/gemspath.sh
...and then open a new shell session.
(This is fixed in Ubuntu 11.10.)
If you happen to have installed Ruby through rbenv, you'll need to execute the following command
rbenv rehash
On macOS I had to add the gem executable directory to the path.
Add these lines to your ~/.bashrc file, and reopen the terminal to refresh the env vars.
# gem
gembin=`(gem env | sed -n "s/.*EXECUTABLE DIRECTORY: \(.*\)/\1/p")`
export PATH=$gembin:$PATH
If you use macOS and you:
I don't know/care about Ruby.
I just want to run this program.
Why is this so complicated?
Then run:
~/.gem/ruby/*/bin/jekyll
where jekyll is the thing you just installed with gem install.
If you're trying to run a simple WEBrick server for your gem you can do the following after installation:
sass start

Ruby: install gem if user does not have it installed

I have a ruby script that I want to send to a couple of coworkers. Instead of telling them to install a few required gems, is there a safe way to have ruby install them if not found?
For example, a user doesn't have the yui-compressor gem. Instead of the terminal displaying an error when they run ruby example.rb it would automatically run gem install -r yui-compressor for them. Is there a way to handle this?
You could use a tool like bundler: http://gembundler.com/

Resources