How to run ruby files? - ruby-on-rails

Example I have the file test.rb:
puts "test test test"
How do I run this file in the ruby console?

load 'test.rb'
Do you mean Rails console? (Same thing, but the question is tagged rails.)

load("test.rb")
should do the trick in irb.

On Mac you can run in three different ways from the terminal
Method 1
On Terminal irb (Interactive Ruby Shell) for line by line execution then quit command to be out of irb.
Method 2
As ruby is Interpreted language we can run by one command on terminal
Save the text editor code with .rb extension.
Change the directory in terminal by cd command (cd drag and drop the folder on terminal so that you can be directed to the directory).
ruby hello.rb
Method 3
ruby -v to know the version of ruby
ruby -e 'puts WVU' #line by line execution on terminal

Any ruby file can be run with just ruby <your_ruby_file.rb>, supposing that you are in the same directory as the ruby file; If not, just provide a path to the file:
ruby path/to/your_file.rb

Related

Difference between command prompts in OS, in RoR, in Ruby, and in node.js

When I type command .. in search bar, it displays
1. Command Prompt
2. Command prompt with Ruby
3. Command prompt with Ruby and Rails.
For running a ruby file, I usually use the command prompt with Ruby and Rails and run the command ruby filename.rb. However, if I use $ sign in front of ruby like this $ruby filename.rb, then it raises an error. Tutorials say to use the command $ruby filename for executing a ruby file. Why does it show $ruby is not recognised?
What is the difference between command prompt, command prompt with Ruby, and command prompt with Ruby and Rails?
While I can run my ruby file with the command ruby filename.rb, why do the tutorials say to use $ before ruby keyword for running a file?
By command prompts in Ruby or in Ruby on Rails, you probably mean a REPL such as irb or pry.
A command prompt (in OS) recognizes shell scripts, while irb or pry recognizes Ruby commands; they recognize different languages.
Different command prompts (or REPL) have different prompts, which helps in identifying which one you are using. When you see $ in instructions, that is a prompt for shell scripts. You don't type the $; it is given, and you type commands after it. In irb or pry, the prompt is >; you don't type that character, but type commands after it.

bin/rails giving error that 'bin' is not recognized as internal

I am a beginner trying to learn and do the Ruby on Rails tutorials (http://guides.rubyonrails.org/getting_started.html#installing-rails)
and for all the bin commands in the command window such as
$ bin/rails --version
or
$ bin/rails server
I am getting the error " 'bin' is not recognized as an internal or external command, operable program or batch file.'
I just installed the latest version of Ruby so I've tried running
rake rails:update:bin
and I also added the PATH variable C:\RailsInstaller\Ruby1.9.3\bin to my environment variables.
I'm still getting the same error.
Thank you in advance!
You are getting those errors because you are on Windows. In this case you have to pass the scripts under the bin folder directly to the Ruby interpreter like this:
ruby bin\rails server
If you have added the bin directory to your PATH variable, then you don't need to use it in the command. Just type $ rails --version, etc.

Rails using gitbash and ruby installer. rails command not found

I am trying to get Ruby on Rails going on my Windows 7 machine. I am using gitbash and have install ruby using ruby installer. All my gems have installed successfully (Rails 3.0.7), but when I try to run a command like rails s I get sh.exe": rails: command not found
echo $PATH gives me:
/c/Users/Dave/bin:.:/usr/local/bin:/mingw/bin:/bin:/c/Ruby192/bin:/c/Program Files/Common Files/Microsoft Shared/Windows Live:/c/Program Files/NVIDIA Corporation/PhysX/Common:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/c/Program Files/Common Files/Adobe/AGL:/c/Program Files/QuickTime/QTSystem/:/c/Program Files/Common Files/Microsoft Shared/Windows Live
Since the path for Ruby is in there I am at a bit of a loss for how to be able to start the rails server.
running gem install rails fixed it
This Problem confuses basic developers. Because they simultaneously use both Git Bash & Command Prompt with Ruby and Rails.
When we type "Rails any command" in CMD with Ruby and Rails, we don't see any failure. But if we run the same command in gitbash means, we get the result "Command not found". Running "Gem install rails" fixs it for gitbash users as the above sayings.
For those who use both Git and Cmd with ROR, they can get the command executed anyhow by running on any of the one...

RoR Question: Where's script/server?

I'm following Head First Ruby, and found out that the server file inside the script folder of my first project is missing. I followed this Ubuntu installation tutorial (sorry, Spanish link, still should be easy to follow the commands) and I guess some files/packages are missing. How I can get them?
antonio#antonio-desktop:~/Documents/tickets$ ruby script/server
ruby: No such file or directory -- script/server (LoadError)
antonio#antonio-desktop:~/Documents/tickets$ ls
app config.ru doc lib public README test vendor
config db Gemfile log Rakefile script tmp
antonio#antonio-desktop:~/Documents/tickets$ cd script
antonio#antonio-desktop:~/Documents/tickets/script$ ls
rails
If you're running Rails 3, it's now:
./script/rails server
All of the separate rails scripts in the scripts directory run through the single ./script/rails dispatcher now. This works for the console and other things you may be looking for as well (ie: ./script/rails console).

/usr/bin/env: ruby 1.8: No such file or

I'm working with Xubuntu 11.04.
I do not succeed to have thin running at server startup.
when I try the command:
/etc/init.d/thin start
I get the following message:
/usr/bin/env: ruby 1.8: No such file or ...
But the commands ruby -v is working fine and the result is ruby 1.8.7 (2011-02-18 patchlevel334) Ruby enterprise
rails -v is also working fine and the result is Rails 2.3.5
If I do the command sudo thin -C configfile.yml start then the thin server is starting without any issue.
Why can't it be started with the server? What can I do to have it working?
The thin script is a usual one which is working fine on a Debian-lenny
I got the some error when using sudo gem install unicorn, and find a solution here. When ruby is not installed in the location /usr/bin/ruby, you need give a full path of ruby which means you need to use command like follows:
/full/path/to/ruby /etc/init.d/thin start
Hope this also works for you.
The header of your file should be:
#!/usr/bin/env ruby
If you include any additional arguments they should be flags for ruby itself, as others may be interpreted as a script name.
What you may have is:
#!/usr/bin/env "ruby 1.8"
Unless you have an executable named ruby 1.8 including the space, that won't work.

Resources