AndrewRadev/switch.vim

Feature request: Ruby lambdas

Closed this issue · 6 comments

Hi,

Not sure if it goes to SplitJoin or Switch or both. Maybe first transformation can be done in Switch and the second one in SplitJoin.

It would be great if I could get rid of the old Ruby lambda syntax lambda { |x| } and convert:

lambda { |x| whatever(x) }

->(x) { whatever(x) }

This is one-way transformation. But the next one should be reversible:

->(x) { whatever(x) }

lambda do |x|
  whatever(x)
end

Please let me know what you think as this might be hard to make.

I've just pushed an implementation of switching between the one-line lambda { |x| } syntax and ->(x) { }. I think this actually solves your problems, since you should be able to split the lambda into a do-end lambda by using splitjoin's standard block-syntax-split. The same thing goes for splitting ->(x) { } into a do-end form, if you feel like it.

Could you play around with this and let me know if it does the trick for you?

So, just to be clear of the process I'm thinking of:

# Start with:
lambda { |x| whatever(x) }
# After switch:
->(x) { whatever(x) }

# Now, let's go the other way

# First, switch
lambda { |x| whatever(x) }
# Then, split from splitjoin:
lambda do |x|
  whatever(x)
end

# And we can go back to the -> syntax with:

# join:
lambda { |x| whatever(x) }
# switch:
->(x) { whatever(x) }

Sorry I am offline right now. Maybe I messed it up myself. I need to check if it's correct. I think multiline lambda in ruby is written using curly braces only.

Doesn't work if it's spelled without space, which I find quite often in old code:

lambda{ ... }

It seems like the issue with the space is fixed now, I'm sorry it took so long to respond. I'm guessing I fixed it a while ago, but who knows.

I think I managed to implement this, and the lambda space issue seems to have been fixed, so I guess I'll go ahead and close this.