Vim and Ruby - matching "do"s and "end"s? - ruby

I'm sure we're all familiar with the "unexpected kEnd" problem. They are always (to me) a lot of trouble to track down, because Vim doesn't match "do"s and "end"s. (And, obviously, other elements that end with "end", like "if")
Is there a way to configure Vim to show these matches to help make debugging easier?

If you are using a recent vim (I'm using 7.4), the built-in matchit macro works well with ruby code.
Simply enable it by adding the following to your .vimrc
runtime macros/matchit.vim
Hit % to match do/end combos and many others in ruby files.

I use this macro and then add this to my vimrc to enable it:
" % to bounce from do to end etc.
runtime! macros/matchit.vim
When I want to find the matching end for a do, I cursor over the do and hit %.

"endwise" is a pretty nice vim extension that adds them automatically, which I found greatly reduces the amount of missing ends. etc:
http://www.vim.org/scripts/script.php?script_id=2386

I use this matchit clone, that can be installed easily with vundle.

Related

Sublime text 3 ugly syntax highlighting

I'm learning Ruby and I'm using Sublime Text 3 but I find the syntax highlighting really strange.
For example :
Ugly syntax
Even after setting the syntax to ruby.
Ruby syntax set
I'd like to know if this is normal, or if I need to change something on the users
settings or something like that.
The syntax file for Ruby (Ruby.sublime-syntax) contains a list of unresolved issues. Among them is:
text:
"p << end
print me!
end"
symptoms:
not recognized as a heredoc
solution:
there is no way to distinguish perfectly between the << operator and the start
of a heredoc. Currently, we require assignment to recognize a heredoc. More
refinement is possible.
• Heredocs with indented terminators (<<-) are always distinguishable, however.
• Nested heredocs are not really supportable at present
So yeah, it's normal.
You could visit https://packagecontrol.io/ and use something like Railscast Colour Scheme
The basic syntax highlighting that comes w/ sublime is pretty sparse - these packages usually do a better job. Also this is just one example. There's plenty of themes and color schemes.
To install package control ctrl+ and past in the snippet according to your version of sublime from this page https://packagecontrol.io/installation#st3

Emacs ruby mode if expressions indentation

Emacs 24 ruby-mode insists on indenting if expressions the following way:
before1 = if params[:before]
Time.zone.at(params[:before].to_i)
end
Which i find just plain wrong. The expected behavior should be:
before1 = if params[:before]
Time.zone.at(params[:before].to_i)
end
That is - the if block should be indented by exactly one level relative to the line in which the if expression starts. Is there any way to achieve this?
If your Emacs is recent enough (24.4+) and you're using the SMIE indentation engine (ruby-use-smie is non-nil), you can use ruby-align-to-stmt-keywords:
(add-to-list 'ruby-align-to-stmt-keywords 'if)
I guess you actually meant to say that Emacs aligns the if with the end, which is actually pretty idiomatic in Ruby (and the style enforced by tools like RuboCop). The second indentation style is popular for method class with blocks, but not for expressions like if/unless/case.
Currently there is now way to change this behaviour. There are plans to introduce a more flexible indentation scheme in ruby-mode in the future, but that's not going to happen in the next Emacs release.
At any rate - it's not a bug, it's a feature :-)

syntax-check a VimL script

I have a sizable vim script (a .vim file, in viml syntax). I'd like to check (but not execute!) the file for simple syntax errors.
How do I accomplish this?
I just want a very rough syntax check. Something along the lines of perl -c or pyflakes.
Here is a syntax checker for VimL.
https://github.com/syngan/vim-vimlint/
I don't think (I'm relatively sure, as much as one can be) one exists. VimL is an internal language of Vim (and only Vim), and there aren't many tools developed for it.
I tried searching on vim.org and several other places, with no luck. Not suprising, because I've never heard of one either.
So you're either stuck with running the script, or switching to an outside language like Python, Perl or Ruby.
https://github.com/osyo-manga/vim-watchdogs
vim-watchdogs, apparently, is a syntax checker for vim, it says that it supports many languages, including vimL
if you use vundle, you can just drop this into your vimrc:
Plugin 'git://github.com/osyo-manga/vim-watchdogs.git'
..and then run:
:PluginInstall
..to set it up (vundle is a very nifty plugin manager) If you have syntastic, you might want to be careful and disable it first, and then see if it is an adequate replacement (since it says it supports all those languages anyway).
It is a safe bet that when you have multiple syntax checkers going, you will need to put your "dogs on a leash", so to speak; by configuring one to check languages that the other one does not, and vice-versa. If you do not, there will be at best collisions, duplications, or misdirections. At worst, you will have all of the above and more.
Make sure that you always backup your ~/.vim directory (or your VIMRUNTIME directory if you install things on a global level), you will be glad you did. Hope that helped you or someone else out, good luck! Sorry you had to wait 7.5 months for a response, heh :)
There's now a second option: vim-lint (as opposed to vimlint)

Ruby debugging and console

can anyone suggest a nice (not Netbeans) platform where i would be able to write ruby code to test?
I find irb a bit hard to follow when you want to define a method more than 3 lines long, and then test it.
Also, maybe just as a wish list item, some way where I could do follow the code step by step to monitor values in variables and make sure things are being done properly?
I'm asking because so far I've been writing in rails, but what you see is the final result of the method, and if you already expected a certain answer, then is fine, but if I need a more complicated method, i would like to be able to follow all the steps to make sure is doing what I want it to do.
Thanks a lot!
a great ide is rubymines by intellij. it is not free though. you can step through code and have the usual ide debugging features.
otherwise if the only problem you have is that you are examining code that is more than 3 lines, you can install the ruby-debug gem and then put the keyword debugger in your code and it will cause a break. So you don't need to put code into irb, just run your ruby script and it will break.
I know you said you find irb a bit hard to follow when you want to define a method more than 3 lines long, but if you use irb -rn ./(your file name here), you will get an irb output of every class, method, module, etc.
irb will walk through line by line so you can see what is working and what is not (you'll get true, false, nil, etc) for each line of code. I've found that you can skip what you already know is working and move on to where you feel the issues are.

Why should I use File.join()?

I wonder why I should use:
puts "In folder #{File.join ENV[HOME], projects}"
Instead of:
puts "In folder #{ENV[HOME]/projects}"
I am aware of that File.join will put the appropriate separator (/ vs \) depending on the OS.
The script is already so tightly tied to what version of ruby you are using, what gems you have installed and so on. My scripts tend not to be like an ORM, (in this case) independent of OS.
I will never run this on Windows (the other dependencies will make the script not to work anyway).
So seems not to be a strong reason for using it, right?
Any of the following :
File.join("first","second")
File.join("first/","second")
File.join("first","/second")
File.join("first/","/second")
Will return
=> "first/second"
Could it be a good reason for you ?
That's only one example I can think of.
Actually, your goal is not to concatenate 2 strings, your goal is creating a path. This looks like a strong reason to use File.join to me.
Haven't used Ruby, but I expect a Path.join to handle corner cases, like paths ending with or without directory separators. Besides, it expresses intent a bit more clearly than string concatenation, and clarity is IMHO almost always a good idea.
I expect join to handle corner cases gracefully, like when ENV[HOME] is empty for some weird reason.
In addition to the other answers your code will be more portable, the correct separator will be used regardless of unix/windows/etc.
be aware of difference between RUBY and PYTHON
RUBY: File.join("","somthing") → "/something"
PYTHON: os.path.join("","somthing") → "something"
RUBY treat empty string as path → I call this a BUG

Resources