ruby commands are not working on my terminal - ruby

so i have recently decided to try and learn ruby. i have a windows 10 hp laptop using git bash (ruby 2.7.1p83) and sublime text editor 3.
i have only made it into week 2 of my ruby intro class but it seems like my computer and the language are not working together.
this is the process;
open git bash
type irb
about 2-3 seconds nothing happens then the next line on git bash puts
switch to inspect mode
krisb#laptop MINGW64
$ irb
SWITCH TO INSPECT MODE
puts hello world
puts hello world
NameError (undefined local variable or method `world' for main:Object)
from (irb):1
from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
from C:/Ruby27-x64/bin/irb:23:in `load'
from C:/Ruby27-x64/bin/irb:23:in `<main>'
if i close out git bash and use the 'winpty' it just sits after I input the command.
krisb#laptop MINGW64
$ alias irb='winpty "$(which irb).cmd"'
>puts hello world [hit enter(moves to next line)]
>

I understand that git-bash gives you the appearance of a Unix terminal environment under Windows which can be handy if you are doing a tutorial targeted at Linux or Mac users, but it has its limitations. Have a look at RubyInstaller (https://rubyinstaller.org/about/comparison/) as it seems to be the closest thing to an official port of Ruby for Windows, and seems to be fairly self-contained and not have too many other dependencies.

Related

ZSH Ruby command not found

I'm pretty inexperienced with terminal type stuff besides the most basic commands, I recently switched from Bash to ZSH with in oh-my-zsh. I'm trying to make an executable ruby script at usr/bin/test.rb. For what it's worth, I never tried this with bash so I have no idea if its zsh specific.
$~ test.rb
/usr/bin/test.rb: line 2: puts: command not found
$~ ruby test.rb
ruby: No such file or directory -- test.rb (LoadError)
and my .zshrc file:
export ZSH=$HOME/.oh-my-zsh
source $ZSH/oh-my-zsh.sh
export PATH="/Users/jason/.rvm/gems/ruby-2.0.0-p247/bin:/Users/jason/.rvm/gems/ruby- 2.0.0-p247#global/bin:/Users/jason/.rvm/rubies/ruby-2.0.0-p247/bin:/Users/jason/.rvm/bin:.git/safe/../../bin:.git/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin:/Users/jason/.rvm/bin"
.
~ which ruby
/Users/jason/.rvm/rubies/ruby-2.0.0-p247/bin/ruby
test.rb
#!/usr/bin/env ruby
puts 'test!'
$~ test.rb
/usr/bin/test.rb: line 2: puts: command not found
This is probably because your script is missing an essential line, often called a "pound-bang line" or, more simply, a "bang line", which tells the operating system what program to use to execute the rest of the file. Typically, for Ruby scripts, it looks like:
#!/usr/bin/ruby
or
#!/usr/bin/env ruby
and MUST be the first line in the file. When the OS opens the file, it looks for #! and, if it sees those, launches the executable at the path given, and passes the script to it. That's basic script execution on a *nix system, and applies to sh/Bash/Perl/Python/Ruby and any number of other executable applications on a *nix system.
ruby test.rb
ruby: No such file or directory -- test.rb (LoadError)
I suspect the second failed because you weren't in the /usr/bin/ directory when you executed that command. Ruby tried to run the script but couldn't find it in the local/current directory.
I'm not trying to be cruel, but, as a programmer, you'll spend a huge amount of time at the command-line, especially so if you are programming in C/C++, Perl, Ruby, Python, or any non-IDE based language. You have to learn how the OS works otherwise disasters of varying sizes and shapes await you, so, in parallel to learning a language you need to learn how to use, and administer, your OS. You don't have to be a power-user or administrator, but you have to know enough to understand good instructions from ones that don't apply, or are just plain-wrong.
Well I was facing the same problem, I had ruby & rails installed but I couldn't run them on ZSH
The answer is so simple
Just Add the following lines to .zshrc
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Then run
source ~/.zshrc
After that to check if ruby & rails are found by ZSH run
ruby --version
rails --verison

Gem not available in self executing ruby file

I have written myself a simple Ruby script that requires the Listen Gem. Running it in the console like this works perfectly.
$ ruby script.rb ARGS
I was always passing the PWD as a argument. I got annoyed of that so I wanted to make the script executable across my linux. So I added the following line to the beginning of my script
#!/usr/bin/env ruby
require "listen"
...
When I now use it as a self executing file, it runs into an error.
$ ./script.rb
Output:
./listen.rb:55: uninitialized constant Listen (NameError)
from ./script.rb:3:in `require'
from ./script.rb:3
Do you have any suggestions what the cause of this problem could be? I have one guess: #!/usr/bin/env ruby calls a different ruby env that $ ruby actually is. But how can I find that out? (I am using rbenv to manage my rubys)

Why won't macvim always use ruby 1.9.3?

I have installed yadr dotfiles, a set of vim, ruby, etc plugins.
I have the following line of Ruby code in a file foo.rb:
foo: bar
Note I used the ruby 1.9.3 syntax for symbol assignment/definition.
When I start macvim from command line using mvim foo.rb and save that file, everything works fine.
However, when I open macvim using open -a macvim and navigate to and open foo.rb, when I try to save the file I get a ruby-vim syntax error on foo: bar. When I change it to :foo => bar I don't get syntax errors.
Using open -a macvim to open macvim, and then entering :!ruby -v prints ruby 1.8.7
Using mvim . to open macvim, and then entering :!ruby -v prints ruby 1.9.3
Depending on how I open macvim, I get a different version of Ruby. How do I ensure that macvim always uses ruby 1.9.3 to evaluate my ruby code?
Thanks
It took me awhile to find a fix, but the issue is caused by MacVim not loading zsh the same way Terminal loads zsh.
The fix is easy enough and can be placed into your zshrc. See a commit from my dotfiles:
https://github.com/simeonwillbanks/dotfiles/commit/e0e19cfeff13f8bc99d8164217ddd84c6d7f9529
The commit references a full explanation which can be found here:
http://vim.1045645.n5.nabble.com/MacVim-and-PATH-tt3388705.html#a3392363
Hope this helps!

How to execute a Ruby script in Terminal?

I've set everything up that I need on my Mac (Ruby, Rails, Homebrew, Git, etc), and I've even written a small program. Now, how do I execute it in Terminal? I wrote the program in Redcar and saved it as a .rb, but I don't know how to execute it through Terminal. I want to run the program and see if it actually works. How do I do this?
Just call: ruby your_program.rb
or
start your program with #!/usr/bin/env ruby,
make your file executable by running chmod +x your_program.rb
and do ./your_program.rb some_param
Open your terminal and open folder where file is saved.
Ex /home/User1/program/test.rb
Open terminal
cd /home/User1/program
ruby test.rb
format or test.rb
class Test
def initialize
puts "I love India"
end
end
# initialize object
Test.new
output
I love India
Assuming ruby interpreter is in your PATH (it should be), you simply run
ruby your_file.rb
To call ruby file use : ruby your_program.rb
To execute your ruby file as script:
start your program with #!/usr/bin/env ruby
run that script using ./your_program.rb param
If you are not able to execute this script check permissions for file.
Just invoke ruby XXXXX.rb in terminal, if the interpreter is in your $PATH variable.
( this can hardly be a rails thing, until you have it running. )
For those not getting a solution for older answers, i simply put my file name as the very first line in my code.
like so
#ruby_file_name_here.rb
puts "hello world"
Although its too late to answer this question, but still for those guys who came here to see the solution of same problem just like me and didn't get a satisfactory answer on this page, The reason is that you don't have your file in the form of .rb extension. You most probably have it in simple text mode. Let me elaborate.
Binding up the whole solution on the page, here you go (assuming you filename is abc.rb or at least you created abc):
Type in terminal window:
cd ~/to/the/program/location
ruby abc.rb
and you are done
If the following error occurs
ruby: No such file or directory -- abc.rb (LoadError)
Then go to the directory in which you have the abc file, rename it as abc.rb
Close gedit and reopen the file abc.rb. Apply the same set of commands and success!
In case someone is trying to run a script in a RAILS environment,
rails provide a runner to execute scripts in rails context via
rails runner my_script.rb
More details here:
https://guides.rubyonrails.org/command_line.html#rails-runner
Open Terminal
cd to/the/program/location
ruby program.rb
or add #!/usr/bin/env ruby in the first of your program (script tell that this is executed using Ruby Interpreter)
Open Terminal
cd to/the/program/location
chmod 777 program.rb
./program.rb
You can run ruby code just passing -e option
ruby -e 'x = Time.now; puts x;'
Output will be:
2022-06-22 15:55:06 +0500

rvm-installed ruby: runs in terminal fine, not anywhere else

I have installed Ruby via RVM on Linux Mint 11. It seems to have installed fine: when I enter type rvm | head -1, I get "rvm is a function" back. Entering ruby -v gives me 1.9.2p290. I can run ruby scripts from the bash terminal window fine. However, when I try to run the same scripts from say gvim (I've got a shortcut mapped to "ruby ") or geany or gedit (ditto), I get "ruby: command not found" (in gvim) or "ruby: not found" (in geany or gedit).
Here's more information: "which ruby" gives me: $HOME/.rvm/rubies/ruby-1.9.2-p290/bin/ruby. $HOME/.rvm/rubies/ruby-1.9.2-p290/bin is in my path in my .bashrc. On another Linux Mint 11 machine, where all works fine, "which ruby" gives me $HOME/.rvm/bin/ruby. Also, on the machine where all is fine, I have a $HOME/bin folder that I don't know where it came from, but was created the same day as my .rvm folder. It contains among other things, links to shell scripts in the $HOME/.rvm/wrappers folder.
I had same problem, you should try making shell login. To make so use the command "bash -l" instead of simply "bash". In geany you can find where to change it in edit->preferences->terminal->shell
I think you dont have your current ruby set as system default. try running this command:
rvm use 1.9.2 --default

Resources