Require statement not working in ruby from bash - ruby

I'm trying to get ttscoff's TaskPaper to Markdown ruby script https://gist.github.com/511174 working. However, I use rvm, which seems to present some challenges.
-rjcode is a no-longer needed Unicode flag and -Ku is another encoding flag I can probably ignore.
I found instructions for adding rvm as a function to your script, but the script still errors as soon as it hits require ftools.
What I added is:
#!/usr/bin/env bash
# TaskPaper to Markdown converter
# Usage: tp2md.rb filename.taskpaper > output.md
# Load RVM into a shell session *as a function*
# Load RVM into a shell session *as a function*
if [[ -s "$HOME/.rvm/scripts/rvm" ]] ; then
# First try to load from a user install
source "$HOME/.rvm/scripts/rvm"
elif [[ -s "/usr/local/rvm/scripts/rvm" ]] ; then
# Then try to load from a root install
source "/usr/local/rvm/scripts/rvm"
else
printf "ERROR: An RVM installation was not found.\n"
fi
# Configure ruby and gemset
rvm ruby-1.9.2-p290#global
ruby <<-rb
puts "Hello!"
rb
Hello! outputs fine, but I get the following errors afterwards:
require: command not found
infile: command not found
prevlevel: command not found
begin: command not found
syntax error near unexpected token `('
` file = File.new(infile, "r")'
My problem seems to be related to the gem not getting pulled in. I've uninstalled ftools and reinstalled with rvm, but still no dice. Thankful for any help!

Your telling ruby to load the 'b' library.
-r library Causes Ruby to load the library using require. It is useful when using -n or -p.
This should work.
ruby -r ftools <<rb
puts "Hello!"
rb

Related

Mac Rspec install fail: -bash: rspec: command not found

I am a newbie and been stuck with this for 2 days; I am about to go crazy.
I am starting to learn Ruby and we got to Rspec.
I installed Rspec with gem install rspec and everything goes ok, but when I execute which rspec I get nothing returned.
When I execute "gem which rspec" I get:
/usr/local/lib/ruby/gems/3.0.0/gems/rspec-3.10.0/lib/rspec.rb
My $PATH is:
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
export PATH="/usr/local/opt/openssl/bin:$PATH"
export PATH="/usr/local/sbin:$PATH"
alias laptop='bash <(curl -s https://raw.githubusercontent.com/monfresh/laptop/master/laptop)'
export PATH="$HOME/.bin:$PATH"
export PATH="/usr/local/opt/ruby/bin:$PATH"
export PATH="/usr/local/lib/ruby/gems/3.0.0/gems/rspec-3.10.0/lib:$PATH"
And "which ruby" throws:
/usr/local/opt/ruby/bin/ruby
I took a look at these two posts but had no luck with them Post 1
,
Post 2
Any help will be really appreciated!

iTerm2 Setup as Login Shell, RVM Still Gives Error

I have been trying to install Ruby-2.6.1 on my Macbook Pro for a couple of days now, and I have followed many posts and blog posts to try to get the setup correct, but I am not successful. I always get the following when trying to set the default Ruby with RVM:
picklemac:~ jblanchard$ rvm use default ruby-2.6.1
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use /bin/bash --login as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.
I have followed the link, but with no success. I have iTerm2 setup as a login shell:
In .bashrc I have the following:
# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
And in .bash_profile I have the following:
# rvm
# export PATH="/Users/jblanchard/.rvm/bin:$PATH"
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
As I said, I have read and tried a number of things to get iTerm2 and my Mac setup properly, including How to make zsh run as a login shell on Mac OS X (in iTerm)?. Also, because I am trying to upgrade from Ruby 2.3 I have read a number of posts on just how to do that - again with no success.
What am I doing wrong?
Additional Info
I am able to successfully list the installations using rvm list:
picklemac:~ jblanchard$ rvm list
ruby-2.6.0 [ x86_64 ]
=* ruby-2.6.1 [ x86_64 ]
# => - current
# =* - current && default
# * - default
This is interesting, because RVM indicates Ruby-2.6.1 is the default, but when I use ruby -v the return is:
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin17]
I was able to successfully make the change to Ruby-2.6.1. First, I uncommented the export line in .bash_profile:
# rvm
export PATH="/Users/jblanchard/.rvm/bin:$PATH" #UNCOMMENTED
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
After stopping and starting iTerm2 I issued the command to use Ruby-2.6.1 and received a warning, but then:
picklemac:~ jblanchard$ rvm use default ruby-2.6.1
Warning! PATH is not properly set up, /Users/jblanchard/.rvm/gems/ruby-2.6.1/bin is not at first place.
Usually this is caused by shell initialization files. Search for PATH=... entries.
You can also re-add RVM to your profile by running: rvm get stable --auto-dotfiles
To fix it temporarily in this shell session run: rvm use ruby-2.6.1
To ignore this error add rvm_silence_path_mismatch_check_flag=1 to your ~/.rvmrc file.
Using /Users/jblanchard/.rvm/gems/ruby-2.6.1
The change to Ruby-2.6.1 was made. It appears I do need to run rvm get stable --auto-dotfiles to fix the warning, which I would prefer to silencing it. Now, when running ruby -v the return is:
ruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin17]

RVM / ZSH - Always shows "Upgraded http://rubygems.org/ to HTTPS" when gem installing

I'm curious why it keeps showing this message any not doing anything about it. Is m RVM is setup incorrectly?
I'm using zsh and have [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function* in my .zprofile
check this files:
~/.gemrc
/etc/gemrc
it has nothing to do with rvm

require ' ' has suddenly stopped working in ruby scripts

I have been using these files all day... And then suddenly this is now what I get.
#!/Users/user969617/.rvm/gems/ruby-1.9.3-p0
puts "hello world"
require 'json'
output:
hello world
rub.rb:3:in `require': no such file to load -- json (LoadError)
from rub.rb:3
I can call it from the interactive ruby shell though... super strange.
Using /Users/user969617/.rvm/gems/ruby-1.9.3-p0
1.9.3p0 :001 > require 'json'
=> true
1.9.3p0 :002 >
Any ideas? I have restarted the computer ;)
------------------ update
I dont use any other require functions, but I just tried require 'Rake' which also returns True in irb, but go the same 'no such file to load'... so it's any require request it seems
You need to bootstrap RVM in your environment. Run this to add the bootstrapping code to your environment.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
If that doesn't fix it, make sure that you have it installed with your current gemset (RVM supports sets of gems which get added and removed from the environment) gem list json If you don't have it, then gem install json

How do I change my Ruby version using RVM?

I am not able to switch the current Ruby version:
➜ ~ rvm list
rvm rubies
ruby-1.9.2-p290 [ x86_64 ]
ruby-1.9.3-p0 [ x86_64 ]
➜ ~ rvm use ruby-1.9.3-p0
RVM is not a function, selecting rubies with 'rvm use ...' will not work.
Fixed it. I needed to add:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM
to .zshrc
This happened to me too. I had:
export PATH=~/.rvm/bin:$PATH
Added in my .bashrc.
All I had to do was add another
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
to the same file and it worked!
Of course, you have to restart your terminal after that.
Your shell doesn't know about the RVM function. After you install it, it tells you how to take care of this. Or go to the install page on the RVM site and check out the section titled "2. Load RVM into your shell sessions as a function"
Run this once to add the line that loads rvm into your ~/.bash_profile:
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
or manually add it yourself. (Note that on some systems, you will want to put it in other places, for example on my system, Mac OSX Lion, I put it in ~/.profile)
(Kubuntu 11.10) The ~/.bash_profile is now called ~/.profile
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.profile
source ~/.profile
rvm info # And now the fields display
To Change the Default Version of ruby:
In Ubuntu 11.10
please change your GNOME terminal setting :
Go to Terminal and then follow the following instructions:
1. Edit > Profile Preferences
2. Open Title and Command Tab
3. Check Run Command as a login Shell
4. Restart terminal
Run this command on terminal:
rvm --default use ruby_Version
To add all RVM functionality to your .bash_profile you should use following command:
echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile
After that you should reload the current shell or open a new terminal session and type the following command to reload .bash_profile:
source .bash_profile
The above solution will only work, if RVM is installed for the current user. A more general solution would use the RVM path variable:
# The following code loads RVM as user or system install:
[[ -s "$rvm_path/scripts/rvm" ]] && . "$rvm_path/scripts/rvm"
I just had to invoke source ~/.bash_profile
On a clean install of Ubuntu 12.04 I ran into the same issue. The RVM installer creates or appends to a file called ~/.bash_login the necessary bit of code to avoid the original problem:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
However this does not seem to get invoked. Adding it to ~/.bashrc resolved the issue for me.
Installing RVM, See here http://octopress.org/docs/setup/rvm/
In my case on Ubuntu, the entry in ~/.bashrc had:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && ."$HOME/.rvm/scripts/rvm" # BAD
instead of:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # WORKING
Notice the missing space between . and "$HOME.
Also, if this is the problem, you should also be noticing an error on top when you start your terminal.
I had a global install of RVM, which runs /etc/profile.d/rvm.sh. However, that script requires the BASH_VERSION or ZSH_VERSION to be set. I was running from crontab, which uses "sh".
I created a wrapper script that uses /bin/bash to source /etc/profile.d/rvm.sh.
You need to change your terminal emulator preferences to allow login
shell. Sometimes it is required to use /bin/bash --login as the
command.

Resources