Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring try! into parity with try. #17361

Merged
merged 1 commit into from Oct 22, 2014
Merged

Bring try! into parity with try. #17361

merged 1 commit into from Oct 22, 2014

Conversation

@aripollak
Copy link
Contributor

aripollak commented Oct 22, 2014

Based on commit 5e51bdd.

Based on commit 5e51bdd.
@chancancode
Copy link
Member

chancancode commented Oct 22, 2014

Wouldn't we need the arity test here?

class A
  def lol(obj)
    obj.try { |thing| call_me_maybe; thing.call_me_maybe }
  end

  def call_me_maybe
    puts 'method in class A'
  end
end

class B
  def call_me_maybe
    puts 'method in class B'
  end
end

A.new.lol(B.new)

I believe that your patch would change this behavior (would be nice if you could add a test like this for both try and try!)

@chancancode
Copy link
Member

chancancode commented Oct 22, 2014

Nvm, I missed that you are just delegating to try

chancancode added a commit that referenced this pull request Oct 22, 2014
Bring try! into parity with try.
@chancancode chancancode merged commit 07b80c1 into rails:master Oct 22, 2014
1 check passed
1 check passed
continuous-integration/travis-ci The Travis CI build passed
Details
@chancancode
Copy link
Member

chancancode commented Oct 22, 2014

In case anyone is wondering about the same thing..

I saw that the original implementation was duplicated from try instead of delegated like what @amatsuda implemented here, so I was wondering if that's done for performance reasons.

Turns out that the difference is very small, almost smaller than what the benchmark library is capable of measuring. If you find anything wrong with my benchmarks feel free to open a new issue/PR 😄

@aripollak
Copy link
Contributor Author

aripollak commented Oct 22, 2014

It occurs to me now that it probably makes more sense to do the delegation the other way around, and just have try do the extra respond_to check. I'll look into making another PR.

@chancancode
Copy link
Member

chancancode commented Oct 22, 2014

@aripollak 👍

@aripollak aripollak deleted the aripollak:try-bang-parity branch Oct 23, 2014
@aripollak aripollak mentioned this pull request Oct 24, 2014
@pirj

This comment has been minimized.

Why do we need to pass *a over once it's empty?

This comment has been minimized.

Copy link

pirj replied Dec 30, 2015

We may also use
try(Proc.new), it should do the same, but we may drop &b from method signature

@pirj

This comment has been minimized.

Why send &b around if no block_given?

This comment has been minimized.

Copy link
Member

kaspth replied Dec 30, 2015

PR welcome 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.