# Ruby style question (many-arg methods)

**URL:** <https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168>\
**Category:** Ruby\
**Created:** [October 30, 2009, 3:18am UTC](https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168 "2009-10-30T03:18:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [October 30, 2009, 3:18am UTC](https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168/1 "2009-10-30T03:18:59Z")

</div>

If you are calling a method that takes a lot of arguments and starts a  
block, how do you make that look pretty?

i.e.

Net::SSH.start(server,user,:password=\>password,:port=\>port,:other\_stuff  
=\> somevariable, :foo =\> bar, :baz =\> qux) do |ssh|

Thats big and ugly and may or may not be \> 80 and/or \> 120 columns. I  
have worked on code that does this:

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux) do |ssh|

Which looks OK, except the code of the block continues on the same  
indentation as the arguments, which (to me) makes it harder to read  
quickly, i.e.

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux) do |ssh|  
foo = foo = new\_foo  
end

(ignore the actual functionality, lets assume noone is actually doing  
something this silly :P)

This to me illustrates the point that its tricky to make out. I have  
worked on a lot of C/C++ code that uses GNU-like style and does this :

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux)  
{  
foo = new\_foo  
}

(just pretend that you can actually start a C block with NET::SSH.start,  
actually this would be something like if  
(complexFunction(blah,blah,blah))

Which looks nice i suppose but… uh… thats not ruby code, with ruby  
you would need a \ after the closing paren, and you’d need to declare  
your variables your passing to the block somewhere, like:

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux)   
{ |ssh|  
foo = new\_foo  
}

or

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux)   
do |ssh|  
foo = new\_foo  
end

Currently I use the last one, because that way at least there is a sort  
of ruby-ish block. But the “conceptual decoupling” of the function from  
the block trips me up occasionally.

Anyway, just got frustrated reading old code and was wondering if anyone  
had a good solution for me ;p

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [October 30, 2009, 3:22am UTC](https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168/2 "2009-10-30T03:22:10Z")

</div>

> Currently I use the last one, because that way at least there is a sort  
> of ruby-ish block. But the “conceptual decoupling” of the function from  
> the block trips me up occasionally.
> 
> Anyway, just got frustrated reading old code and was wondering if anyone  
> had a good solution for me ;p

Oops, wrote this all out of whack, currently I use this:

Net::SSH.start(  
server,  
user,:  
password=\>password,  
:port=\>port,  
:other\_stuff =\> somevariable,  
:foo =\> bar,  
:baz =\> qux) do |ssh|  
foo = new\_foo  
end

But thats a weird half-indentation thing and I dno how I feel about it,  
would maybe work if ruby indentatino were 4-space. I’ve tried the last  
one (above) recently but the aforementioned decoupling trips me up

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [October 30, 2009, 3:37am UTC](https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168/3 "2009-10-30T03:37:12Z")

</div>

Well, you should realize that leading whitespace on each new line is  
removed in emails and the like. So use some web service instead, like  
pastie. I assume you mean you use this method currently:  
[http://pastie.org/676220](http://pastie.org/676220)

Well, I might do something like this (but I’m sure many would consider  
that ugly - just a personal preference really): [http://pastie.org/676224](http://pastie.org/676224)

---

<div class="post-metadata">

**Author:** ![paerez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@paerez](https://www.ruby-forum.com/u/paerez)\
**Post date:** [October 30, 2009, 4:39am UTC](https://www.ruby-forum.com/t/ruby-style-question-many-arg-methods/178168/4 "2009-10-30T04:39:40Z")

</div>

Ehsanul H. wrote:

> Well, you should realize that leading whitespace on each new line is  
> removed in emails and the like. So use some web service instead, like  
> pastie. I assume you mean you use this method currently:  
> [http://pastie.org/676220](http://pastie.org/676220)
> 
> Well, I might do something like this (but I’m sure many would consider  
> that ugly - just a personal preference really): [http://pastie.org/676224](http://pastie.org/676224)

Ah, yes I was using this via firefox not as a ML, and firefox preserved  
the indenting. Your pastie guess is spot on though, thanks 🙂

I have used the method you pastied for a long time, I switched partially  
because for some big-ole-method-calls I like to add  
comments-per-argument.
