What to do when you want to know the source position where the method is defined in binding.pry

Introduction

When reading the ruby code

--Too little knowledge about the implicit part ――It's too complicated to understand

Sometimes I use a debugger.

However, in rare cases, I wanted to step in a method, but the description that I thought was an argument of that method was actually another method call, and I couldn't understand the call tree.

At that time, source_location that grows in class Method I finally came up with the idea of using the method, so make a note of it.

Break

When running rspec, I tried to replace the redis client used in ʻActiveSupport :: Cache :: RedisCacheStore with a mock, but there is no method called with`, and an example of investigating the cause of the failure.

Line 345 becomes method_missing with redis.with. Looking at the redis gem, it seems that with is not defined. I wonder what.

From: /bundle/gems/activesupport-6.0.3.2/lib/active_support/cache/redis_cache_store.rb:344 ActiveSupport::Cache::Strategy::LocalCache#read_entry:

    339:         # Store provider interface:
    340:         # Read an entry from the cache.
    341:         def read_entry(key, **options)
    342:           failsafe :read_entry do
    343:             binding.pry
 => 344:             raw = options&.fetch(:raw, false)
    345:             deserialize_entry(redis.with { |c| c.get(key) }, raw: raw)
    346:           end
    347:         end
    348:
    349:         def read_multi_entries(names, **options)

Stop replacing the mock, re-execute, and try to view the source.

[1] pry(#<ActiveSupport::Cache::RedisCacheStore>)> redis.method(:with).source
=> "      def with\n        yield self\n      end\n"

What is this method? In such a case, source_location!

[2] pry(#<ActiveSupport::Cache::RedisCacheStore>)> redis.method(:with).source_location
=> ["/bundle/gems/activesupport-6.0.3.2/lib/active_support/cache/redis_cache_store.rb", 24]

Is it here? https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/activesupport/lib/active_support/cache/redis_cache_store.rb#L24-L26

Is it easy to use by including ConnectionPoolLike and patching Redis? I wonder if I have to include ConnectionPoolLike in the redis client that I plug in with a mock. If the implementation of ActiveSupport changes, I wonder if I can follow it properly.

Like.

Q

If classes are opened in various places and the behavior can be changed, it may be difficult to notice just by reading the source. Finished.

Recommended Posts

What to do when you want to know the source position where the method is defined in binding.pry
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do when you want to delete a migration file that is "NO FILE"
What is ... (3 dots) found in the Java source? Variadic arguments to know from
What to do when "Fail to load the JNI shared library" is displayed in Eclipse
What to do when Method not found in f: ajax
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
[Rails] What to do when the view collapses when a message is displayed with the errors method
ProxyFactory is convenient when you want to test AOP in Spring!
What to do when the changes in the Servlet are not reflected
What to do if Cloud9 is full in the Rails tutorial
What to do if you forget the root password in CentOS7
[Swift] When you want to know if the number of characters in a String matches a certain number ...
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
What to do when javax.el.ELException: Not a Valid Method Expression: appears when the JSF screen is displayed
What is the main method in Java?
What to do when undefined method ʻuser_signed_in?'
[IOS] What to do when the image is filled with one color
What to do if the prefix c is not bound in JSP
What to do if tomcat process remains when tomcat is stopped in eclipse
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
What to do when debugging "Source not found"
What do you use when converting to String?
What to do when IllegalStateException occurs in PlayFramework
What to do when you think you can't do Groovy-> Java in IntelliJ IDEA CE
What to do when the value becomes null in the second getSubmittedValue () in JSF Validator
What to do when rails db: seed does not reflect in the database
You may not want to use the remove method in ArrayList very often
Androd: What to do about "The Realm is already in a write transaction in"
What to do if you get an "A server is already running." Error when you try to start the rails server
[Rails] I want to reset everything because the data in the local environment is strange! What to do before that
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
What to do if you get a wrong number of arguments error in binding.pry
What to do when The SSL certificate has expired
What to do if you can't get the text of an element in Selenium
What to do when Address already in use is displayed after executing rails s
When you want to dynamically replace Annotation in Java8
If you want to recreate the instance in cloud9
ParseException: What to do when Unparseable date is reached
[Maven] What to do if you are asked to incorporate a jar that is not in the remote repository into the war
[Joke] What to do when the invalid sample rate is reached when using Mixxx on Ubuntu 20.04
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
Use JLine when you want to handle keystrokes on the console character by character in Java
[Rails] What to do if you can't get an error message with the errors method
What you need to do to open a file from the menu in the document-Based App macOS app
What to do when Rails on Docker does not reflect controller changes in the browser
When you want to reflect the Master Branch information in the Current Branch you are currently working on
What to do if you get a java.io.IOException in GlassFish
What to do when you launch an application with rails
What to do when Cannot apply expression operators to method binding
When you want to change the MySQL password of docker-compose
Delegate is convenient to use when you want to reuse parts
What to do if you can't use the rails command
What is the pluck method?
What is the initialize method?
What to do when "relation" hibernate_sequence "does not exist" in the ID column of PostgreSQL + JPA
[Rails] What to do when the error No database selected and Unknown database appears in db: migrate
[Programming beginner] What to do when rails s becomes an error in the local development environment