Ruby editing style rules and recommendation?

N

Nathan Oyama

I asked a question 'Decimal in for loop?' and two members were kind
enough to solve this problem. Thanks, Fleck and Aaron.

Now I have another question: Are there any editing style rules and
recommendations in Ruby?

In the previous thread, Fleck wrote:

0.step(0.5,0.1) { |i| p i }

while Aaron wrote:

(0..0.5).step(0.1) do |f|
p f
end

and both work identically.

Which one is recommended in terms of the editing style?

In C++, for example, I use Ellemtel rules
(www.doc.ic.ac.uk/lab/cplus/c++.rules)
Emacs supports editing assistance in this style particularly
auto-indent. . . I'm sure that Emacs supports the similar feature in
Ruby too because Matz loves Emacs! Please advise.

Thanks,
Nathan
 
I

Iñaki Baz Castillo

El S=C3=A1bado, 9 de Enero de 2010, Nathan Oyama escribi=C3=B3:
In the previous thread, Fleck wrote:
=20
0.step(0.5,0.1) { |i| p i }
=20
while Aaron wrote:
=20
(0..0.5).step(0.1) do |f|
p f
end
=20
and both work identically.
=20
Which one is recommended in terms of the editing style?


Imho using { } for inline code and do..end for a multiline block of code. T=
his=20
is:


Correct:

array.each do |entry|
lalala =3D entry.name=20
lololo =3D entry. time
end

Correct:

array.each { |entry| lalala =3D entry.name }

"Incorrect":

array.each { |entry|
lalala =3D entry.name=20
lololo =3D entry. time
}

"Incorrect":

array.each do |entry| lalala =3D entry.name ; end



=2D-=20
I=C3=B1aki Baz Castillo <[email protected]>
 
P

Phillip Gawlowski

I asked a question 'Decimal in for loop?' and two members were kind
enough to solve this problem. Thanks, Fleck and Aaron.

Now I have another question: Are there any editing style rules and
recommendations in Ruby?

In the previous thread, Fleck wrote:

0.step(0.5,0.1) { |i| p i }

while Aaron wrote:

(0..0.5).step(0.1) do |f|
p f
end

and both work identically.

Which one is recommended in terms of the editing style?

Whatever fits the situation. No, really. If the {...} block form fits in
an instance, you use that. If the do...end block form fits, you use that
instead.

Caveat: The latter is most common if you deal with a multi-line block:

a_block do |x|
puts x
x += 1
end

Otherwise:
Indents are two spaces. Not tabstops of length 2, but the actual
character (" ").

And once you have written enough Ruby, you'll know what "feels" correct
to you.
 
R

Rick DeNatale

I asked a question 'Decimal in for loop?' and two members were kind
enough to solve this problem. Thanks, Fleck and Aaron.

Now I have another question: Are there any editing style rules and
recommendations in Ruby?

In the previous thread, Fleck wrote:

0.step(0.5,0.1) { |i| p i }

while Aaron wrote:

(0..0.5).step(0.1) do |f|
=A0p f
end

and both work identically.

Which one is recommended in terms of the editing style?

In C++, for example, I use Ellemtel rules
(www.doc.ic.ac.uk/lab/cplus/c++.rules)
. Emacs supports editing assistance in this style particularly
auto-indent. . . I'm sure that Emacs supports the similar feature in
Ruby too because Matz loves Emacs! Please advise.

Here's my two cents

http://talklikeaduck.denhaven2.com/2007/10/02/ruby-blocks-do-or-brace



--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Which one is recommended in terms of the editing style?

I usually use braces for single line, and do/end for multi-line.

I've also started alternating them to make it clearer where blocks start and
end.

def whatever
each { |obj|
if obj < MID
call_method
else
self.var = yield obj
call_other_method
end
}
end

def whatever
each do |obj|
if obj < MID
call_method
else
self.var = yield obj
call_other_method
end
end
end

I think the top one is much easier to read, so I sometimes alternate now.
 
M

Marnen Laibow-Koser

Josh said:
I usually use braces for single line, and do/end for multi-line.

That's what 99% of Ruby programmers do. :)
I've also started alternating them to make it clearer where blocks start
and
end.

def whatever
each { |obj|
if obj < MID
call_method
else
self.var = yield obj
call_other_method
end
}
end

Yuck! That looks inconsistent to me. It would also be an editing
nightmare if you extract or introduce one level of nesting.
def whatever
each do |obj|
if obj < MID
call_method
else
self.var = yield obj
call_other_method
end
end
end

I think the top one is much easier to read, so I sometimes alternate
now.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
(e-mail address removed)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Yuck! That looks inconsistent to me. It would also be an editing
nightmare if you extract or introduce one level of nesting.

It's not mandatory, it's mostly just whether they can be lined up easily.
Meaning I would do it if I were having difficulty reading it, so at that
point it can be expected to save me time. If the level of nesting changes,
that doesn't translate into a change between braces and do/end, only if the
introduction makes it difficult to read (meaning they alternate based on
readability rather than every other level of nesting).
 
N

Nathan Oyama

Thanks, your all are really helpful!
As a newbie, I will try to write Ruby codes as NORMALLY as possible :)
 
A

Albert Schlef

Marnen said:
Josh said:
def whatever
each { |obj|
if obj < MID
call_method
else
self.var = yield obj
call_other_method
end
}
end

[...] It would also be an editing
nightmare if you extract or introduce one level of nesting.

Why?

How would you usually add one level of nesting there? (perhaps I could
learn an editing trick.)
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

It makes it conspicuous where everything begins and ends, you don't have to
analyze anything to figure it out.

How would you usually add one level of nesting there? (perhaps I could
learn an editing trick.)
I don't think I understand the question, you write it the same way you
normally would, you just replace some do/end with brackets, where they make
it easier to follow. The criteria are subjective.

I'm not sure what you mean by an editing trick, presumably you're thinking
as Marnen did, that adding a level obligates me to swap anything back and
forth. I don't consider that to be the case, it's not a rule, it's just
something I do to clarify my code, sometimes I find two do/end pairs between
every bracket is easiest to read, it just depends. But if you're in
TextMate, you can do ctrl+{ to swap back and forth. It seems to work okay,
but is a little buggy, so I don't use it often.
 
L

Lars Christensen

0.step(0.5,0.1) { |i| p i }

while Aaron wrote:

(0..0.5).step(0.1) do |f|
=A0p f
end

Which one is recommended in terms of the editing style?

Whichever makes the code more readable in each situation.

I tend to use braces/{ } in functional-style code and do/end in
imperative-style code.

For example, I usually use braces with
Enumerable#map/select/delete_if/find/sort, and do/end with
Enumerable#each, File#open and Thread#new.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top