How to navigate to method definition for ruby files in Emacs.

335 views
Skip to first unread message

srihari k

unread,
Sep 24, 2011, 1:57:19 PM9/24/11
to Bangalore Ruby User Group
Hey all,

In emacs what do you use to go to definition of a method ?

I am trying to start using emacs for my day to day development.(using this my conf now - https://github.com/mig/emacs-rails-kit/ ).Before this I was using netbeans , in netbeans there you could ctrl+click a method to navigate to its declaration.

Thanks in advance.
--
srihari

Noufal Ibrahim

unread,
Sep 24, 2011, 2:06:59 PM9/24/11
to bangal...@googlegroups.com
On Sat, Sep 24, 2011 at 11:27 PM, srihari k <sri...@gmail.com> wrote:
> Hey all,
>
> In emacs what do you use to go to definition of a method ?

I mostly do Python work (not Ruby) and I usually rely on memory to
tell me where something is defined. If it's in a 3rd party library
which I'm not familiar with, I use etags[1] which is not totally
reliable for dynamic languages but hasn't failed me till now.


Footnotes:
1. http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/etags.html
--
~noufal
http://nibrahim.net.in

srihari k

unread,
Sep 24, 2011, 2:29:25 PM9/24/11
to bangal...@googlegroups.com
On Sat, Sep 24, 2011 at 11:36 PM, Noufal Ibrahim <nou...@gmail.com> wrote:
On Sat, Sep 24, 2011 at 11:27 PM, srihari k <sri...@gmail.com> wrote:
> Hey all,
>
> In emacs what do you use to go to definition of a method ?

I mostly do Python work (not Ruby) and I usually rely on memory to
tell me where something is defined. If it's in a 3rd party library
which I'm not familiar with, I use etags[1] which is not totally
reliable for dynamic languages but hasn't failed me till now.

Thanks , set up ctags and using  this along with it http://rinari.rubyforge.org/Navigation.html , but would like to know how people browse third party code ( generate TAGS for every gem ? )
 
--
srihari

Srushti

unread,
Sep 24, 2011, 2:31:07 PM9/24/11
to BANGALORE RUG-Ruby Users Group
Yeah, I've used ctags on Vim (and I use it for my own code too, but
more to get to where I want to go quickly rather than not remembering
where it is). Make sure to use exuberant ctags (I know Mac comes with
a different versions which doesn't really do the job).

Also, check out cscope, you can use it do things like find usages of a
method or a class. I know there's a plugin for Vim, and there almost
certainly has to be one for Emacs.

Of course, the caveats Noufal mentioned about dynamic languages would
apply.

Srushti
http://c42.in

On Sep 24, 11:06 pm, Noufal Ibrahim <nou...@gmail.com> wrote:

Srushti

unread,
Sep 24, 2011, 2:37:51 PM9/24/11
to BANGALORE RUG-Ruby Users Group
Unfortunately, yes... I generate ctags (& the cscope db). I have a
line like below in my .zshrc file and I just run it as and when I need
it (just the first part relates to ctags).

alias refreshctags="ctags -f tags --recurse=yes . && find . -name
'*.rb' -o -name '*.java' -o -name '*.cs' -o -name '*.js' -o -name
'*.haml' -o -name '*.erb' >| cscope.files && cscope -b -q"

Srushti
http://c42.in

On Sep 24, 11:29 pm, srihari k <srih...@gmail.com> wrote:
> On Sat, Sep 24, 2011 at 11:36 PM, Noufal Ibrahim <nou...@gmail.com> wrote:
> > On Sat, Sep 24, 2011 at 11:27 PM, srihari k <srih...@gmail.com> wrote:
> > > Hey all,
>
> > > In emacs what do you use to go to definition of a method ?
>
> > I mostly do Python work (not Ruby) and I usually rely on memory to
> > tell me where something is defined. If it's in a 3rd party library
> > which I'm not familiar with, I use etags[1] which is not totally
> > reliable for dynamic languages but hasn't failed me till now.
>
> Thanks , set up ctags and using  this along with ithttp://rinari.rubyforge.org/Navigation.html, but would like to know how
> people browse third party code ( generate TAGS for every gem ? )
>
> --
> srihari
>
>
>
>
>
>
>
> > Footnotes:
> > 1.http://www.gnu.org/software/emacs/emacs-lisp-intro/html_node/etags.html
> > --
> > ~noufal
> >http://nibrahim.net.in

Nithin Bekal

unread,
Sep 24, 2011, 2:38:21 PM9/24/11
to bangal...@googlegroups.com
Sorry to post off topic but does anyone here use Redcar? If so, do you use any plugins for the functionality Srihari mentioned?

srihari k

unread,
Sep 24, 2011, 2:45:02 PM9/24/11
to bangal...@googlegroups.com


On Sun, Sep 25, 2011 at 12:07 AM, Srushti <srs...@gmail.com> wrote:
Unfortunately, yes... I generate ctags (& the cscope db). I have a
line like below in my .zshrc file and I just run it as and when I need
it (just the first part relates to ctags).

alias refreshctags="ctags -f tags --recurse=yes . && find . -name
'*.rb' -o -name '*.java' -o -name '*.cs' -o -name '*.js' -o -name
'*.haml' -o -name '*.erb' >| cscope.files && cscope -b -q"


This seems the only way..(well how else would emacs know about methods in gems). Will probably use someting like this.

Anyways, waiting for some emacs user to respond.

--
srihari

Punit Rathore

unread,
Sep 24, 2011, 5:34:43 PM9/24/11
to bangal...@googlegroups.com
exuberant-ctags works well with emacs too. You can get is from here - http://ctags.sourceforge.net/

Then you build the tag definitions specific to your project with this command - (i've renamed the ctags binary which I installed from the above URL, to ctags-exuberant, because a different version of ctags was installed by default)

ctags-exuberant -a -e -f <path-to-tags-file> --tag-relative -R <folder1> <folder 2>

Eg: ctags-exuberant -a -e -f TAGS --tag-relative -R lib vendor
(This will generate a TAGS file in the current directory)

Then fire up emacs, and you can select the TAGS file with this command : M-x visit-tags-table
Then you can look up tags definition with M-x . (meta-x dot).

Also everytime you make changes to your functions, you will have to rebuild the tag definitions again by executing the above command.

The lookup doesn't work 100% of the time, but its better than not having any tag definitions. Hope this helps :)

Punit Rathore

Kumar Gaurav

unread,
Sep 25, 2011, 12:41:23 AM9/25/11
to bangal...@googlegroups.com
On Sat, Sep 24, 2011 at 11:27 PM, srihari k <sri...@gmail.com> wrote:
> Hey all,
>
> In emacs what do you use to go to definition of a method ?
>

I use M-x find-grep-dired which works like find in files.
To find the method definition I type def <method_name>

I am thinking that one can streamline this using keyboard macros [1]
so that it will combine all the above steps into one macro which you
have to execute (and maybe bind to <your_fav_key>) to go to the
definition of the method.

[1] http://www.emacswiki.org/emacs/KeyboardMacros


--
Kumar Gaurav
Latest Post -- Definitive Guide to accepts_nested_attributes_for in rails 3
http://wp.me/pbU2Q-4v

Jasim A Basheer

unread,
Sep 25, 2011, 1:47:25 AM9/25/11
to bangal...@googlegroups.com
FWIW, JetBrains' RubyMine has pretty decent support for jump to
method, amongst other IDEish stuff like live syntax checking, inline
documentation, autocomplete etc.

--
Regards,
Jasim A Basheer.
C42 Engineering, http://c42.in
http://jasimabasheer.com

Habibullah Pagarkar

unread,
Sep 25, 2011, 2:05:55 AM9/25/11
to bangal...@googlegroups.com
Good suggestions here.

Srihari, if you are looking for an out of the box solution with either Emacs or Vim, I don't think you'll find it. Be prepared to spend a bit of time diving into some Elisp code to customize the environment to your needs. Funnily enough, that's precisely why folks like using Emacs or Vim.

However if you are new to Ruby and Rails then you may not want to add this conceptual overhead. In that case, I wholeheartedly recommend RubyMine.

Habib
--
Habibullah Pagarkar
habibp...@gmail.com

http://www.cs.jhu.edu/~habib
http://habib.posterous.com/
http://twitter.com/mhabibp

hemant

unread,
Sep 25, 2011, 2:26:55 AM9/25/11
to bangal...@googlegroups.com
I keep a instance of RubyMine running - just for navigating the Rails
code! I find it too slow for actual coding.

Having said that. For C/C++ or moderately small Ruby projects,
excuberant ctags works well enough. So, I keep following snippet in my
.emacs:

(setq path-to-ctags "/usr/local/bin/ctags") ;; <- you're ctags path here

(defun create-tags (dir-name)
"Create tags file."
(interactive "DDirectory: ")
(shell-command
(format "%s -f %s/TAGS -e -R %s" path-to-ctags dir-name dir-name))
)

(global-set-key "\C-ct" 'visit-tags-table)
(global-set-key "\C-cd" 'create-tags)

And use "M-." for visiting the definition of word under the cursor and
"M-*" to pop back. Works well enough, unless we are talking about
painfully meta-programmed code base. :)

There is also,

http://code.google.com/p/google-gtags/

I have heard at least one guy saying that, he managed to make it work
with Ruby/Rails. But honestly, did not look too closely.

--
Let them talk of their oriental summer climes of everlasting
conservatories; give me the privilege of making my own summer with my
own coals.

http://gnufied.org
http://twitter.com/gnufied

ganesh kumar

unread,
Sep 25, 2011, 1:53:38 AM9/25/11
to bangal...@googlegroups.com
+1 Rubymine

On Sun, Sep 25, 2011 at 11:17 AM, Jasim A Basheer <jasi...@gmail.com> wrote:



--
The only safety in life is to enjoy the risk of living.

Habibullah Pagarkar

unread,
Sep 25, 2011, 2:49:28 AM9/25/11
to bangal...@googlegroups.com
Hemant, I find RubyMine 3.2.4 to be quite fast as long as it isn't opening a 1000 line file. :D But then speed is relative.

Thanks for the link to gtags. Will play with that soon.

srihari k

unread,
Sep 25, 2011, 2:47:36 AM9/25/11
to bangal...@googlegroups.com
On Sun, Sep 25, 2011 at 3:04 AM, Punit Rathore <pun...@gmail.com> wrote:
exuberant-ctags works well with emacs too. You can get is from here - http://ctags.sourceforge.net/

Then you build the tag definitions specific to your project with this command - (i've renamed the ctags binary which I installed from the above URL, to ctags-exuberant, because a different version of ctags was installed by default)

       ctags-exuberant -a -e -f <path-to-tags-file> --tag-relative -R <folder1> <folder 2>

       Eg: ctags-exuberant -a -e -f TAGS --tag-relative -R lib vendor
       (This will generate a TAGS file in the current directory)

Then fire up emacs, and you can select the TAGS file with this command : M-x visit-tags-table
Then you can look up tags definition with M-x .   (meta-x dot).

Also everytime you make changes to your functions, you will have to rebuild the tag definitions again by executing the above command.

The lookup doesn't work 100% of the time, but its better than not having any tag definitions. Hope this helps :)


Yes doing this right now.
I am thinking of generating tags for  the gems in Gemfile.

--
srihari

Jasim A Basheer

unread,
Sep 25, 2011, 2:50:07 AM9/25/11
to bangal...@googlegroups.com
Nice approach!

RubyMine (or any other Java IDE for that matter - NetBeans/Eclipse) is
pretty heavy. I found it too slow to be of any use in my macbook.
However for development I'm using a quad-core desktop with 8gb RAM
which takes the sting out a little bit.

About customization, I've found the default features available in
RubyMine to be well-aligned with my requirements.

I also found Vim with vim-rails, NerdTree, CommandT, Exuberant CTags
and Ack integration pretty neat for Rails development. I'm using
Srushti's VimGet bundle (https://github.com/srushti/vim-get). I
presume a lot of Ruby devs are using Carlhuda's Janus
(https://github.com/carlhuda/janus) happily.

I've never worked with emacs before. How good is the Rails editing
ecosystem in emacs compared to vim?

On Sun, Sep 25, 2011 at 11:56 AM, hemant <geth...@gmail.com> wrote:

Habibullah Pagarkar

unread,
Sep 25, 2011, 2:57:32 AM9/25/11
to bangal...@googlegroups.com


On Sun, Sep 25, 2011 at 12:20 PM, Jasim A Basheer <jasi...@gmail.com> wrote:
<snip>


I've never worked with emacs before. How good is the Rails editing
ecosystem in emacs compared to vim?
 
<chop>

I've not used Vim a lot, but I do get the impression that Vim's Rails customizations are ahead of Emacs.

hemant

unread,
Sep 25, 2011, 3:29:01 AM9/25/11
to bangal...@googlegroups.com
Hi,

On Sun, Sep 25, 2011 at 12:19 PM, Habibullah Pagarkar
<habibp...@gmail.com> wrote:
> Hemant, I find RubyMine 3.2.4 to be quite fast as long as it isn't opening a
> 1000 line file. :D But then speed is relative.
>
> Thanks for the link to gtags. Will play with that soon.

Yup agreed speed is relative. Let me know, if you manage to get gtags
going for Ruby, that will be interesting.

srihari k

unread,
Sep 25, 2011, 3:24:37 AM9/25/11
to bangal...@googlegroups.com

I wrote up this ugly thing :

ctags-exuberant -a -e -f TAGS --tag-relative -R app lib vendor `bundle list | awk '/\*/ {print $2}'|xargs -I {} bundle show {}|xargs`

and M-. in has_attached_file in user.rb took me to paperclip.rb .So works as expected for now.

--
srihari

 
--
srihari

Noufal Ibrahim

unread,
Sep 25, 2011, 8:41:47 AM9/25/11
to bangal...@googlegroups.com
On Sun, Sep 25, 2011 at 3:04 AM, Punit Rathore <pun...@gmail.com> wrote:
[..]

> Also everytime you make changes to your functions, you will have to rebuild the tag definitions again by executing the above command.

You can automate this using inotifywait (if you're on Linux).
Check this out http://superuser.com/questions/181517/how-to-execute-a-command-whenever-a-file-changes/181543#181543

You might want to watch the directory instead of the file so that you
can track multiple ones.

--
~noufal
http://nibrahim.net.in

Reply all
Reply to author
Forward
0 new messages