N
Nick Green
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,assword=>password,ort=>port,ther_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,
ort=>port,
ther_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,
ort=>port,
ther_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 )
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,
ort=>port,
ther_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,
ort=>port,
ther_stuff => somevariable,
:foo => bar,
:baz => qux) \
{ |ssh|
foo = new_foo
}
or
Net::SSH.start(
server,
user,:
password=>password,
ort=>port,
ther_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
block, how do you make that look pretty?
i.e.
Net::SSH.start(server,user,assword=>password,ort=>port,ther_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,
ort=>port,
ther_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,
ort=>port,
ther_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 )
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,
ort=>port,
ther_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,
ort=>port,
ther_stuff => somevariable,
:foo => bar,
:baz => qux) \
{ |ssh|
foo = new_foo
}
or
Net::SSH.start(
server,
user,:
password=>password,
ort=>port,
ther_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