Bring try! into parity with try. #17361

Merged
merged 1 commit into from Oct 22, 2014

Conversation

Projects
None yet
4 participants
@aripollak
Contributor

aripollak commented Oct 22, 2014

Based on commit 5e51bdd.

@chancancode

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Oct 22, 2014

Member

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!)

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

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Oct 22, 2014

Member

Nvm, I missed that you are just delegating to try

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

@chancancode chancancode merged commit 07b80c1 into rails:master Oct 22, 2014

1 check passed

continuous-integration/travis-ci The Travis CI build passed
Details
@chancancode

This comment has been minimized.

Show comment
Hide comment
@chancancode

chancancode Oct 22, 2014

Member

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 😄

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

This comment has been minimized.

Show comment
Hide comment
@aripollak

aripollak Oct 22, 2014

Contributor

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.

Contributor

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

This comment has been minimized.

Show comment
Hide comment
Member

chancancode commented Oct 22, 2014

@aripollak 👍

@aripollak aripollak deleted the aripollak:try-bang-parity branch Oct 23, 2014

@aripollak aripollak referenced this pull request Oct 24, 2014

Merged

DRY up try/try! #17377

@pirj

This comment has been minimized.

Show comment
Hide comment
@pirj

pirj Dec 30, 2015

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

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

This comment has been minimized.

Show comment
Hide comment
@pirj

pirj Dec 30, 2015

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

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.

Show comment
Hide comment
@pirj

pirj Dec 30, 2015

Why send &b around if no block_given?

Why send &b around if no block_given?

This comment has been minimized.

Show comment
Hide comment
@kaspth

kaspth Dec 30, 2015

Member

PR welcome 😁

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