Giter VIP home page Giter VIP logo

Comments (5)

westonganger avatar westonganger commented on July 22, 2024

Sorry I guess I need to change the description of that limitation. It appears to break only when the method does not exist on the class. For example:

Array&.bad_method 
#=> NoMethodError: undefined method `bad_method' for Array:Class

Class&.to_s&.class&.bad_method
#=> NoMethodError: undefined method `bad_method' for String:Class

Let me know if you know how to get around this without killing any performance

from fast_try.

engineersmnky avatar engineersmnky commented on July 22, 2024

There is nothing you can do without impacting performance of the safe operator because all additions would require method calls. I think the simplest solution if you really intended to make it more similar to ActiveSupport#try would be:

self&.public_send(a,*b,&c) if respond_to?(a) 

This nearly identical to how ActiveSupport handles it in try

from fast_try.

westonganger avatar westonganger commented on July 22, 2024

Yeah that was the only other solution I had determined. However then it will no longer behave like safe navigation (and I dont think it needs the safe navigation anymore either).

from fast_try.

engineersmnky avatar engineersmnky commented on July 22, 2024

the safe navigation only offers assistance to nil so it would work in the identical fashion it currently does excepting the fact that it will return nil if the object does not respond to the method name rather than raising a NoMethodError.

e.g.

def simple_test(obj,method_name)
    if obj.respond_to?(method_name)
       puts "#{obj.inspect} responds to #{method_name}"
       obj&.public_send(method_name) 
   end
end


simple_test(nil,:to_s) 
# nil responds to to_s
#=> nil 
simple_test("a",:to_s)
# a responds to to_s
#=> "a"
simple_test(
     simple_test("a",:non_existent) # this will return nil
,:to_s)
# nil responds to to_s
#=> nil 

Safe navigation is not meant to be a replacement for try but rather it is meant to make it "safe" to call methods when you are expecting an object that you expect to respond to the method call or nil. eg

h = {r: 12}
h[:r]&.to_i
#=> 12
h[:p]&.to_i
#=> nil

Here I know that Hash#[] will return an object (which I expect to respond to to_i) or it will return nil if the key does not exist

from fast_try.

westonganger avatar westonganger commented on July 22, 2024

Hmm interesting, however to_s is a bad use case for this method but its a limitation none-the-less.

Given all the differences it seems I may change the name of this repo to something else.

from fast_try.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.