Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby convention for chaining calls over multiple lines

What are the conventions for this?

I use the folowing style, but not sure it is the preferred one since if I miss a dot at the end I can run into a lot of issue without realising that.

query = reservations_scope.for_company(current_company).joins{property.development}.   group{property.development.id}.   group{property.development.name}.   group{property.number}.   group{created_at}.   group{price}.   group{reservation_path}.   group{company_id}.   group{user_id}.   group{fee_paid_date}.   group{contract_exchanged_date}.   group{deposit_paid_date}.   group{cancelled_date}.   select_with_reserving_agent_name_for(current_company, [                                        "developments.id as dev_id",                                        "developments.name as dev_name",                                        "properties.number as prop_number",                                        "reservations.created_at",                                        "reservations.price",                                        "reservations.fee_paid_date",                                        "reservations.contract_exchanged_date",                                        "reservations.deposit_paid_date",                                        "reservations.cancelled_date"   ]).reorder("developments.name") query.to_a # .... 

So what are the conventions for chaining methods over multiple lines and which one should I prefer?

NOTE: I couldn't find a good example from the Ruby coding style guide.

like image 705
Dmytrii Nagirniak Avatar asked Oct 16 '12 00:10

Dmytrii Nagirniak


2 Answers

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.

like image 198
Bozhidar Batsov Avatar answered Oct 08 '22 01:10

Bozhidar Batsov


In Ruby 1.9+ it's possible to write like this:

query = reservations_scope   .for_company(current_company)   .joins{property.development}   .group{property.development.id}   .group{property.development.name}   .group{property.number}   .group{created_at}   .group{price}   .group{reservation_path}   .group{company_id}   .group{user_id} 

Much more readable, I think.

like image 39
detunized Avatar answered Oct 08 '22 01:10

detunized