Ruby convention for chaining calls over multiple lines

There is actually a section on that in the Ruby style guide:

Adopt a consistent multi-line method chaining style. There are two
popular styles in the Ruby community, both of which are considered
good – leading . (Option A) and trailing . (Option B).

  • (Option A) When continuing a chained method invocation on
    another line keep the . on the second line.

    # bad - need to consult first line to understand second line
    one.two.three.
      four
    
    # good - it's immediately clear what's going on the second line
    one.two.three
      .four
    
  • (Option B) When continuing a chained method invocation on another line,
    include the . on the first line to indicate that the
    expression continues.

    # bad - need to read ahead to the second line to know that the chain continues
    one.two.three
      .four
    
    # good - it's immediately clear that the expression continues beyond the first line
    one.two.three.
      four
    

A discussion on the merits of both alternative styles can be found
here.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)