Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Ruby editing style rules and recommendation? (http://www.velocityreviews.com/forums/t861209-ruby-editing-style-rules-and-recommendation.html)

Nathan Oyama 01-09-2010 08:01 PM

Ruby editing style rules and recommendation?
 
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
--
Posted via http://www.ruby-forum.com/.


I├▒aki Baz Castillo 01-09-2010 08:10 PM

Re: Ruby editing style rules and recommendation?
 
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 <ibc@aliax.net>


Phillip Gawlowski 01-09-2010 08:15 PM

Re: Ruby editing style rules and recommendation?
 
On 09.01.2010 21:01, Nathan Oyama wrote:
> 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.

--
Phillip Gawlowski


Rick DeNatale 01-09-2010 09:35 PM

Re: Ruby editing style rules and recommendation?
 
On Sat, Jan 9, 2010 at 3:01 PM, Nathan Oyama <nate@culip.net> wrote:
> 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/2...ks-do-or-brace



--=20
Rick DeNatale

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


Josh Cheek 01-10-2010 01:47 AM

Re: Ruby editing style rules and recommendation?
 
[Note: parts of this message were removed to make it a legal post.]

On Sat, Jan 9, 2010 at 2:01 PM, Nathan Oyama <nate@culip.net> wrote:

> 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.


Marnen Laibow-Koser 01-10-2010 02:34 AM

Re: Ruby editing style rules and recommendation?
 
Josh Cheek wrote:
> On Sat, Jan 9, 2010 at 2:01 PM, Nathan Oyama <nate@culip.net> wrote:
>
>> Which one is recommended in terms of the editing style?
>>

>
> 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
marnen@marnen.org

--
Posted via http://www.ruby-forum.com/.


Josh Cheek 01-10-2010 09:27 AM

Re: Ruby editing style rules and recommendation?
 
[Note: parts of this message were removed to make it a legal post.]

On Sat, Jan 9, 2010 at 8:34 PM, Marnen Laibow-Koser <marnen@marnen.org>wrote:

> > 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.
>


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).


Nathan Oyama 01-11-2010 12:38 AM

Re: Ruby editing style rules and recommendation?
 
Thanks, your all are really helpful!
As a newbie, I will try to write Ruby codes as NORMALLY as possible :)
--
Posted via http://www.ruby-forum.com/.


Albert Schlef 01-11-2010 06:49 AM

Re: Ruby editing style rules and recommendation?
 
Marnen Laibow-Koser wrote:
> Josh Cheek wrote:
>> 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.)
--
Posted via http://www.ruby-forum.com/.


Josh Cheek 01-11-2010 12:51 PM

Re: Ruby editing style rules and recommendation?
 
[Note: parts of this message were removed to make it a legal post.]

On Mon, Jan 11, 2010 at 12:49 AM, Albert Schlef <albertschlef@gmail.com>wrote:

> Why?
>
>

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.



All times are GMT. The time now is 12:31 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.