How to load ruby file in the pry editor? - ruby

I use pry with the standard editor vi.
Whenever i type 'edit' inside pry, vi shows up. That's good.
And with "load 'file'" i can run a ruby file in the pry session but when i type edit, the content of the file is not available in the vi editor of the pry session.
Is there a way to load a ruby file directly in the editor within pry?

As mudasobwa pointed out:
It's simple like 'edit myfile.rb'
and I end up with vi editing the file requested.

Related

Are there vi / vim shortcuts available in pry or irb?

I have muscle memory of the vi/vim commands. So in bash, I use the vi mode,
for example, I can easily go back to my command history and re-edit previous commands.
Are vi shortcuts available with pry or irb? If so, how do I set it up?
Thank you.
Not sure about re-editing previous commands, but you could use interactive editor gem to start vim-like editing from inside of your irb. Using that, you can start irb, edit your script in vim and let ruby shell execute it immediately. Here is a great tutorial on this: Running Vim within IRB.

File name completion in pry

I'm switching irb to pry. I just found that file name completion in pry didn't work.
I have a foo.rb in current directory, then I can complete file name like irb> load 'f[TAB], but this completion doesn't work in pry.
Is there configuration for this, or it is not possible to use file name completion in pry?
You can use "shell-mode" or "file-mode (alias for shell-mode)" in pry.

Quick navigate to the definition code of a function from another ruby gem in Emacs using ctags

I have a ruby script in emacs.
require 'watir'
ie=Watir::IE.new
When I puts the cursor on the word IE and press C+] , I want Emacs to open the definition of the IE class, which is in the source code of the watir gem.
How can I achieve that using Emacs and ctags?
Use the following command it the directory where your gems are installed:
ctags -e -a --Ruby-kinds=-fF -o TAGS -R .
Afterwards press M-. in Emacs while your cursor is over the Watir word. Emacs will prompt you for the location of the TAGS file and afterwards it will jump to the definition of the type.
Basic etags usage in Emacs:
M-. goes to the method under the cur­sor, in the same win­dow. First time it asks for the TAGS file.
C-4 . goes to the method under the cur­sor. Opens a new win­dow. First time it asks for the TAGS file.
M-, cycles to the next selection.

How do you save IRB inputs to a .rb file?

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

How to write ruby code easier?(I mean in terminal write and then run it)

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.

Resources