ruby-on-rails - 类的实例方法
问题描述
我想看看类实例(类路径名)所独有的方法。我愿意:
Rails.root.class.methods(false)
我得到:
[:getwd, :pwd, :glob]
然后,我调用其中一种方法:
Rails.root.pwd
我得到:
NoMethodError (undefined method `pwd' for #<Pathname:/Users/borjagvo/workstation/npilookup>)
为什么我会收到此错误?如何仅查看 Pathname 实例的公共方法?
谢谢!
解决方案
你必须在类上调用类方法,比如
Rails.root.class.pwd
OR
Pathname.pwd
获取所有实例方法(没有祖先也没有单例):
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
推荐阅读
- regex - 需要使用 html 对正则表达式进行更精细的调整
- gridstack - GridStack - 重新初始化现有网格
- javascript - 异步减少返回承诺
- angular - 如果多个条件为真,哪种结构指令更适合仅显示一个元素?
- sql-server - SQL Server 存储过程,语法无效
- python - Matplotlib 设置固定的 y 轴值并重命名它们
- c++ - SDL中screenSurface->format是什么意思?
- google-cloud-platform - Gcloud命令行结构
- excel - 如何编写 VBA 循环来为客户编号列表运行多个模块?
- c# - 实体框架自动安全导航