splitjoin.vim icon indicating copy to clipboard operation
splitjoin.vim copied to clipboard

Support for switching ruby lambda literals

Open glittershark opened this issue 9 years ago • 9 comments

It'd be cool if we could switch between

-> (a, b) { a.do_stuff_with(b) }

and

lambda do |a, b|
  a.do_stuff_with(b)
end

Though there's gonna have to be some fiagling to make it not overlap with the block-syntax splitjoiner

(thanks for the awesome plugin, btw :smile:)

glittershark avatar Feb 20 '15 15:02 glittershark

Sorry it took me a while to get to this.

It would be possible to transform the arrow-block into a lambda, but I'm not sure under what circumstances we want which. I guess both syntaxes are perfectly valid for both one-line and multiline formats, right? Is this a matter of coding style?

If it is, one thing I'm considering is adding an option, similar to the one in #67. Maybe something like:

" Arrow lambda when splitting, but normal "lambda" when joining
let g:splitjoin_ruby_arrow_lambdas = 'Sj'

" Arrow lambda when joining, but normal "lambda" when splitting
let g:splitjoin_ruby_arrow_lambdas = 'sJ'

Does this look like a reasonable option to you? Or do you think it should always be one or the other for some reason? Or is it different depending on circumstances (in which case, maybe it could be added as a rule in switch.vim instead, so you can switch between them on a case-by-case basis)?

(thanks for the awesome plugin, btw :smile:)

Thanks for the kind words :).

AndrewRadev avatar Apr 21 '15 15:04 AndrewRadev

This is wrong. Multi-line lambda in ruby is written like this:

lambda { |x|
...
}

Not with do end like blocks.

firedev avatar Jun 10 '15 14:06 firedev

that's just like, your opinion, man

glittershark avatar Jun 10 '15 14:06 glittershark

https://github.com/bbatsov/ruby-style-guide#single-line-blocks

glittershark avatar Jun 10 '15 14:06 glittershark

https://github.com/bbatsov/rubocop/blob/master/lib/rubocop/cop/style/block_delimiters.rb#L44

glittershark avatar Jun 10 '15 14:06 glittershark

Oh right, sorry, it's only when you pass multiline lambda as a parameter. For example when using scope in Rails. You need to surround it with () or use curly braces otherwise the block will be treated as a parameter for scope.

It just messed up in my head.

firedev avatar Jun 10 '15 14:06 firedev

yeah, you're right on that.

glittershark avatar Jun 10 '15 15:06 glittershark

Sorry, again, for taking so long to address this.

For starters, there was a separate issue related to this, actually: https://github.com/AndrewRadev/switch.vim/issues/27

What I implemented there was a way to toggle between the arrow style and lambdas. That said, it would still be a useful tool to have in splitjoin, I assume not many people use the lambda syntax anyway. I'll leave this open and hope I can get to this a bit sooner this time.

AndrewRadev avatar May 21 '16 12:05 AndrewRadev

Small correction just in case:

I assume not many people use the lambda syntax anyway

Ruby style guide recommends using lambda for multiline blocks: https://github.com/bbatsov/ruby-style-guide#lambda-multi-line.

astyagun avatar Mar 04 '17 18:03 astyagun