Ruby editing style rules and recommendation?

Discussion in 'Ruby' started by Nathan Oyama, Jan 9, 2010.

  1. Nathan Oyama

    Nathan Oyama Guest

    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/.
     
    Nathan Oyama, Jan 9, 2010
    #1
    1. Advertising

  2. 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 <>
     
    Iñaki Baz Castillo, Jan 9, 2010
    #2
    1. Advertising

  3. 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
     
    Phillip Gawlowski, Jan 9, 2010
    #3
  4. On Sat, Jan 9, 2010 at 3:01 PM, 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|
    > =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
     
    Rick DeNatale, Jan 9, 2010
    #4
  5. Nathan Oyama

    Josh Cheek Guest

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

    On Sat, Jan 9, 2010 at 2:01 PM, Nathan Oyama <> 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.
     
    Josh Cheek, Jan 10, 2010
    #5
  6. Josh Cheek wrote:
    > On Sat, Jan 9, 2010 at 2:01 PM, Nathan Oyama <> 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Marnen Laibow-Koser, Jan 10, 2010
    #6
  7. Nathan Oyama

    Josh Cheek Guest

    [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 <>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).
     
    Josh Cheek, Jan 10, 2010
    #7
  8. Nathan Oyama

    Nathan Oyama Guest

    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/.
     
    Nathan Oyama, Jan 11, 2010
    #8
  9. 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/.
     
    Albert Schlef, Jan 11, 2010
    #9
  10. Nathan Oyama

    Josh Cheek Guest

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

    On Mon, Jan 11, 2010 at 12:49 AM, Albert Schlef <>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.
     
    Josh Cheek, Jan 11, 2010
    #10
  11. On Sat, Jan 9, 2010 at 9:01 PM, Nathan Oyama <> wrote:
    > 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.
     
    Lars Christensen, Jan 13, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. fj
    Replies:
    5
    Views:
    410
    Paul W
    Jul 17, 2005
  2. Replies:
    0
    Views:
    614
  3. Brian

    Python and Outlook-style rules

    Brian, Sep 9, 2011, in forum: Python
    Replies:
    2
    Views:
    403
    Brian
    Sep 10, 2011
  4. Ken Varn
    Replies:
    0
    Views:
    530
    Ken Varn
    Apr 26, 2004
  5. Frank Carr
    Replies:
    0
    Views:
    100
    Frank Carr
    Dec 10, 2003
Loading...

Share This Page