Emacs Ruby mode & indentation with the tab key wish/bug

Heya,

I have a wish for the indentation with the tab key in the Ruby mode. I'm
currently using

(defconst ruby-mode-revision "$Revision: 1.74.2.1 $")

from Debian/unstable's ruby1.8-elisp package. So here's where (IMHO !) the
indentation with the tab key is not working properly:

1) Continued statements should be indented with only ruby-indent-level
spaces. At the moment it produces something like this:

myvar = 2 + 3 +
        4 + 1

I'd like to have it behave more like

myvar = 2 + 3 +
  4 + 1

assuming that ruby-indent-leve is 2. This might be desputable, so maybe it
should be an option.

2) Indentation with { } blocks is wrong. It does work nicely with e.g.
upto:

0.upto(2) { |i|
  print("i is #{i}\n")
}

but not with "if...":

if (i == 2) {
    asdasd
  }

After this passage tab indents to column 3, so this case is definitely
broken. Replacing { and } with begin/then and end fixes this, of course,
but I prefer { }.

Thanks for considering it.

Mosu

···

--
If Darl McBride was in charge, he'd probably make marriage
unconstitutional too, since clearly it de-emphasizes the commercial
nature of normal human interaction, and probably is a major impediment
to the commercial growth of prostitution. - Linus Torvalds

Hi

Moritz Bunkus wrote:

1) Continued statements should be indented with only ruby-indent-level
spaces. At the moment it produces something like this:

myvar = 2 + 3 +
        4 + 1

I'd like to have it behave more like

myvar = 2 + 3 +
  4 + 1

I think there's an already option for what you want, I think; the option's called ruby-deep-arglist, which you'll want to switch OFF to use your preferred style. Try the following in your .emacs

(add-hook 'ruby-mode-hook
       '(lambda ()
       (custom-set-variables
        '(ruby-deep-arglist 0))
))

2) Indentation with { } blocks is wrong. It does work nicely with e.g.
upto:

....

but not with "if...":

if (i == 2) {
    asdasd
  }

After this passage tab indents to column 3, so this case is definitely
broken.

Um, the case is broken, but I don't think it's the syntax highlighter :). You don't enclose conditional blocks in { } in Ruby - the 'if' (or 'unless' or 'elsif' or 'else') marks the beginning of the block itself, and you must use 'end' to mark the end of the block. You also don't usually need parens round the conditional statement, unlike Perl. So:

if i == 2
   puts "foo"
end

Aah ... isn't that lovely and readable?

Cheers
alex

Hi,

I think there's an already option for what you want, I think; the
option's called ruby-deep-arglist, which you'll want to switch OFF to
use your preferred style.

Doesn't help. In fact I don't see a difference with these three cases
whatever I set that variable to:

mylist = ["1", "2",
          "3", "4"]

if (("a" == "b") and
      ("c" == "d")) then
end

myvar = 3 + 4 +
        1 + 5

So I guess that either it's not working correctly or it's not supposed
to change these cases in which case I'd still wish for it to be
implemented :slight_smile:

Um, the case is broken, but I don't think it's the syntax highlighter
:). You don't enclose conditional blocks in { } in Ruby

Ok. I'm obviously a Ruby n00b :wink: Anyway, it's kinda confusing to be able
to use { } for blocks in one case but not the other. What's the reason
behind that?

You also don't usually need parens round the conditional statement,
unlike Perl.

That's just a matter of style / preference. I'll probably continue using
them 'cause I'd like to keep my programming style at least partially
equal no matter if I code in C/C++, Perl or Ruby.

if i == 2
  puts "foo"
end

Aah ... isn't that lovely and readable?

Sure it is. It's just nice having Emacs highlight the brace the current
one corresponds to. A kind of 'visual theres-the-start-of-this-block
marker'.

Anyway, thanks for the help.

Mosu

···

--
If Darl McBride was in charge, he'd probably make marriage
unconstitutional too, since clearly it de-emphasizes the commercial
nature of normal human interaction, and probably is a major impediment
to the commercial growth of prostitution. - Linus Torvalds

Moritz Bunkus wrote:

I think there's an already option for what you want, I think; the option's called ruby-deep-arglist, which you'll want to switch OFF to use your preferred style.

Doesn't help. In fact I don't see a difference with these three cases
whatever I set that variable to:

mylist = ["1", "2",
          "3", "4"]

if (("a" == "b") and
      ("c" == "d")) then
end

myvar = 3 + 4 +
        1 + 5

Sorry, my mistake. The variable you want is ruby-deep-indent-paren. Set it to false to get the style you're looking for. That definitely works for me on your first and third examples.

Um, the case is broken, but I don't think it's the syntax highlighter :). You don't enclose conditional blocks in { } in Ruby

Ok. I'm obviously a Ruby n00b :wink: Anyway, it's kinda confusing to be able
to use { } for blocks in one case but not the other. What's the reason
behind that?

Someone else will be able to give you a better explanation than me, but as I understand it:

1) There's "blocks" of code that start with keywords like "begin", "if", "else", "rescue" that are just that - code that is executed on the given condition / exception / whatever. This is similar to blocks as I know 'em in Perl.

2) Then, there are ruby blocks marked with 'do .. end' or { .. } that are actually arguments to methods called on objects. This is a distinctive and delightful feature of Ruby. A classic method that receives a ruby block is Array#each, which calls the associated block for each member of the array successively. You can write your own methods that receive blocks, either by using 'yield' or by receiving them as the last argument in the method definition.

class Array
   def each_in_reverse
     reverse.each { | item | yield item }
   end
end

a = [ 1, 2, 3 ]
a.each_in_reverse { | x | p x * 3 } # 9, 6, 3

This is a pretty inane example, but hopefully gives you an idea. Take a look at the chapter 'Containers, Blocks and Iterators', in the Pragmatic Programmers' "Programming Ruby" book for a good discussion.

The choice between do .. end and { ..} is largely a matter of taste. There is a difference in precedence, and a very common idiom is to prefer do .. end for multi-line blocks and { .. } for single-line ones.

You also don't usually need parens round the conditional statement,
unlike Perl.

That's just a matter of style / preference.

Not in Perl it isn't :slight_smile:

I'll probably continue using
them 'cause I'd like to keep my programming style at least partially
equal no matter if I code in C/C++, Perl or Ruby.

That's fine - I'm sure no-one from c.l.r. will be coming round your house late at night to check :slight_smile:

It's just nice having Emacs highlight the brace the current
one corresponds to. A kind of 'visual theres-the-start-of-this-block
marker'.

Sure. I tend to rely more on the indentation to do that for me, but YMMV.

Happy rubying - cheers
alex

Hi,

Sorry, my mistake. The variable you want is ruby-deep-indent-paren. Set
it to false to get the style you're looking for. That definitely works
for me on your first and third examples.

Works like a charme here, too. Thanks :slight_smile:

Someone else will be able to give you a better explanation than me, but
as I understand it:

...

Ok that makes sense. Thanks for the explanation & the examples.

>That's just a matter of style / preference.

Not in Perl it isn't :slight_smile:

Of course not, I meant Ruby :wink:

That's fine - I'm sure no-one from c.l.r. will be coming round your
house late at night to check :slight_smile:

Not!? Damn! All the money I've already spent on my security
measurements... totally wasted...

Mosu

···

--
If Darl McBride was in charge, he'd probably make marriage
unconstitutional too, since clearly it de-emphasizes the commercial
nature of normal human interaction, and probably is a major impediment
to the commercial growth of prostitution. - Linus Torvalds