Might sound like a newbie question (and it is since I am new to Ruby and IRB) but is there a way to save the commands you did in IRB to file? I am playing with WATIR and would love to save all my inputs to file instead of copying and pasting each.
Thanks for the help!
On my machine I can put this in my .irbrc file (located in your home directory):
Kernel.at_exit {
File.open("irb.log", "w") do |f|
f << Readline::HISTORY.to_a.join("\n")
end
}
It creates a file irb.log that contains your readline history. Irb uses readline for command input. It might be configured not to use readline for some people, I don't know. And maybe the history will be truncated at some point, or maybe it'll be modified by certain commands you do in your irb session... but try it out and see if it works.
If you want the irb prompt and the result of each command to be included in the log, then just use tee to record the output of irb:
$ irb | tee irb.log
You can run vim in irb:
http://vimcasts.org/episodes/running-vim-within-irb/
Take a look at watir-console.
I found this question when looking to do the same thing. I ended up switching from IRB to Pry; it is a separate REPL project for Ruby that has a whole host of advanced features not supported in IRB.
Well worth a look.
Pry
Related
Good afternoon! I have a problem with Ruby. I downloaded this program from the official site https://www.ruby-lang.org/en/downloads/ on Fedora without any problems (using the command via the terminal). However, I cannot use the 'irb' command in the terminal to run Ruby on Linux as described here https://www.ruby-lang.org/en/documentation/quickstart/. Therefore, I cannot learn the Ruby programming language.Command 'irb' not found
I tried my best to run Ruby in other ways. One of them, as is customary in Fedora, is to write the name of the program in the terminal. But it opens a folder in which, I assume, Ruby is located (I can't find the folder itself either in Files or via the 'cd' command in the terminal). I kind of can open this folder in the terminal, but I can't find it in Files.
Oh, by the way, the 'irb' command doesn't work there. Only some real command for a program like this one will work: irb(main):001:0> "Hello World" (from https://www.ruby-lang.org/en/documentation/quickstart/). The program is and is not at the same time
Move on. I made a text document, wrote irb(main):001:0> "Hello World" in it (as written in "Ruby in Twenty Minutes"), and saved the file as hello.rb . Opened the hello.rb file path through the terminal and wrote 'ruby hello.rb' (without quotes, of course). Result: syntax error. hello.rb file Tried to open hello.rb via ruby in terminal
Oh yeah, I almost forgot: when I tried to open hello.rb in Files through another application, there was no Ruby script in the list (ruby or irb or at least something through which one could open a .rb file). No program for .rb format
I hope I have explained the problem in sufficient detail and clearly. And I hope for your help and understanding! In any case, good mood to you!
On Fedora & CentOS, the Ruby installation is split into many smaller packages. If you want to run irb, you'll also have to install the ruby-irb package using dnf install ruby-irb.
Total newb here. I'm trying to get a simple ruby program to run in the terminal on my MacBook Pro. I used Atom text editor to write the following:
class Sample
def hello
puts "Hello, World!"
end
end
s = Sample.new
s.hello
I saved the file as my_program.rb to a folder on my desktop. I go to the terminal to run the program. I type
ruby my_program.rb
and it returns
ruby: No such file or directory -- my_program.rb (LoadError)
I can use the irb and run a single line of ruby using
ruby -e 'puts "hello world"'
But can't get it to find the .rb file.
I appreciate any help y'all can offer! Thanks!
ruby ~/Desktop/my_program.rb
Ruby might be clever, but is has no mind-reading builtin. You need to tell it, where your file is. This is, of course, not Ruby-specific, but applies to all commands - they can't guess, where in your file system you have stored a file.
An alternative would be to place ~/Desktop in your PATH and use
ruby -S my_program.rb
Ruby will then execute the first program with this name which it finds in $PATH. Whether it is wise to place the Desktop directory into the PATH is a different issue....
Here is my ruby script:
require_relative 'lib/say.rb'
say("hello")
I run it from macvim via :w ! ruby and got:
:w ! ruby
-:1:in `require_relative': cannot infer basepath (LoadError)
from -:1:in `<main>'
shell returned 1
Though ! ruby % vim command works fine.
And also changing the require_relative... to require File.expand_path('../lib/say', __FILE__) also helps...
But... What is actually wrong with these friends: require_relative + :w ! ruby ?
Why do I need this? Just to use in vim the ⌘ + R shortkey from the vim-ruby-runner plugin which is based on :w !ruby vim command and have my code looks better with require_relative...
P.S.
I found the following question on SO: Why isn't current directory on my Ruby path?
Seems the latter source correspond to my problem too... But it was hard for me to get all answers I need from there.
The owner of the vim-ruby-runner plugin, found the solution at http://www.rodrigoalvesvieira.com/add-require-relative-to-irb/
Plugin is fixed and can be get at https://github.com/henrik/vim-ruby-runner
Solution (and fix for vim-ruby-runner) can be also found at https://github.com/henrik/vim-ruby-runner/issues/5
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
When I write a little ruby code, after a little bit, I always need to create a new terminal tab to ruby it, to see if it's correct.
Are there any ways to do it in one window? Like a vim plugin or some other tool?
The following should work in vim, after you've saved the file:
:!ruby %
Or even
:!%
This works under Linux when you have the correct "shebang" as the first line of the ruby file:
#!/usr/bin/env ruby
For extra fun, you can map this to a key in your ~/.vimrc:
map <F8> :!ruby %<CR>
Do you mean you need an interpreter to see what your code does? If so, check out irb.
The way you should check if your code works is using unit tests, not running it in the console or irb. Indeed, irb is a good solution for small fragment of code or to check for specific statements.
However, there are some solutions to your specific question.
You can write the code in a file, save it and run it from the console.
ruby filename.rb
If you use TextMate, you can press ⌘ + R to execute the current code
Do as Simone Carletti said.
And for editing and saving your file suggest you Scite.
http://www.scintilla.org/SciTEDownload.html here you can download it for many different operating systems.
You get syntax highlighting in a very lightweight editor for almost everything (html, ruby, eruby, xml,...).
But you will need to have at least a Window Manager running.
in ~/.vimrc
autocmd FileType ruby imap <F8> <C-o>:w <CR> <C-o> :!ruby % <CR>
this way you can save and execute your file at once within insertion mode
In vim:
:!ruby %
will execute ruby on the current file. Remember to save it first!
If you are the Emacs type you should check out ruby-mode (which IIRC was written by Matz) and inf-ruby. See e.g. http://lathi.net/pages/emacs-ruby
You don't say what OS you're using, so I'm assuming either Linux or Mac-OS.
When you're at a command-line and using vim (not gvim) you can do a <CNTRL>+Z to temporarily halt the editor and return to the command-line. Issue any commands you need, then use "fg" to return to the editor.
There are times I'll use :!ruby % from inside vim (or gvim) but sometimes I need the real command line and if I'm ssh'd into a machine the <CNTRL>+Z trick is nice.
Agreed with #Simone Carletti. If you are learning the language and want to make sure that methods/classes are doing what you want then you can use irb.
There is a gem called interactive_editor which enable you to run vim inside irb (side-by-side actually). Watch this Vimcast for demo.