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 ;) 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 :)
> 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 :)
> 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