0

I want to see what methods are unique to a class instance (class Pathname). I do:

Rails.root.class.methods(false)

And I get:

[:getwd, :pwd, :glob]

Then, I call one of the methods:

Rails.root.pwd

And I get:

NoMethodError (undefined method `pwd' for #<Pathname:/Users/borjagvo/workstation/npilookup>)

Why do I get this error? How can I see the public methods for instances of Pathname only?

Thanks!

1
  • 2
    Does Rails.root.class.instance_methods(false) do anything for you? Commented Nov 4, 2020 at 12:21

2 Answers 2

2

You have to call class methods on class, like

Rails.root.class.pwd

OR

Pathname.pwd

For getting all instance methods (no ancestors nor singleton):

pry(main)> Rails.root.class.instance_methods(false) # OR Pathname.instance_methods(false)
=> [:write, :join, :+, :/, :as_json, :directory?, :exist?, :readable?,
    :readable_real?, :world_readable?, :writable?, :writable_real?, 
    :world_writable?, :executable?, :executable_real?, :file?, :size? .....]
# Then you can call above methods on instance of a Pathname
 pry(main)> Rails.root.size?
 => 1248
Sign up to request clarification or add additional context in comments.

Comments

1

You are fetching the methods from the class of Rails.root, i.e. Pathname's class methods:

Rails.root.class        #=> Pathname
Pathname.methods(false) #=> [:glob, :getwd, :pwd

To get the instance's methods, use:

Rails.root.methods
#=> [:mountpoint?, :root?, :each_filename, :descend, :ascend, :write, ...]

Or, to get its singleton methods: (there are none, it's an ordinary Pathname)

Rails.root.methods(false)
#=> []

Note that Rails.root is an instance of Pathname and already represents your app's working directory:

Rails.root
#=> #<Pathname:/Users/borjagvo/workstation/npilookup>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.