新手问题 有没有方法可以获取定义的 def 方法中的内容求大神帮帮忙小弟还在敲代码工作

rc13520822803 · May 16, 2018 · Last by IChou replied at May 17, 2018 · 1538 hits

😭内容、正则都不重要请忽略,,主要是有没有自带方法获取 passive 函数中的全部内容? 比如

def passive
   m=[]
   if @body =~ /<p>Powered by <a href="http:\/\/http://www.axigen.com[\/]*" target="_blank">Axigen Mail Server/i
      m << { :version=>@body.scan(/<p>Version ([\d\.]+)<\/p>/)[0][0] }
   end
   m
end
```ruby

发帖时,先调整排版下,按照论坛规格发帖,不然会被无视、拉进小黑屋的

def passive
end

😭 我发的时候手动排版了啊,我错了。

require 'method_source'
puts self.method(:passive).source.display
Reply to teddyinfi

这个有问题吧 method(:passive) 返回的是一个 Method 的对象,Method 的对象有 :source 这个方法?

Reply to IChou

try it

method(:passive).source #class method
instance_method(:passive).source #instance method
Reply to teddyinfi

不需要 try, 官方 api 里面确实没有这个的,估计你也是在 pry 中吧,你在 irb 中试试

大神你们先讨论,,我去 try it,,

Reply to IChou

就欣赏你这种自信~

Reply to teddyinfi

dash 在手,天下我有 😜 你也值得拥有

@teddyinfi @IChou 输出看了看:

[:each_index, :join, :rotate, :rotate!, :sort!, :sort_by!, :collect!, :map!, :select!, :keep_if, :values_at, :delete_at, :to_h, :delete_if, :reject!, :transpose, :include?, :rassoc, :uniq!, :assoc, :compact, :compact!, :flatten!, :shuffle!, :shuffle, :fill, :permutation, :combination, :sample, :repeated_permutation, :repeated_combination, :flatten, :bsearch, :product, :bsearch_index, :&, :*, :+, :-, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :any?, :pack, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle, :sum, :uniq, :|, :insert, :index, :rindex, :<=>, :<<, :clear, :replace, :==, :[], :[]=, :empty?, :eql?, :reverse, :reverse!, :concat, :max, :min, :inspect, :length, :size, :each, :delete, :to_ary, :slice, :slice!, :to_a, :to_s, :dig, :hash, :frozen?, :at, :fetch, :last, :push, :pop, :shift, :unshift, :find, :entries, :sort_by, :grep, :grep_v, :detect, :find_all, :flat_map, :collect_concat, :inject, :reduce, :partition, :group_by, :all?, :one?, :none?, :minmax, :min_by, :max_by, :minmax_by, :member?, :each_with_index, :each_entry, :each_slice, :each_cons, :each_with_object, :chunk, :slice_before, :slice_after, :slice_when, :chunk_while, :lazy, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :define_singleton_method, :method, :public_method, :remove_instance_variable, :instance_variable_set, :singleton_method, :extend, :to_enum, :enum_for, :===, :=~, :!~, :respond_to?, :freeze, :object_id, :send, :display, :nil?, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]

真的好像没有

@rubyist518 大概意思是把 passive 方法放到 class 类中,然后用类的 method 输出 source?百度找不到 source 什么鬼,求指教。

你这个最终目的是想做到什么? 如果把最终目的说出来,说不定有其他更好更简单的方案

require 'method_source'
puts self.method(:passive).source.display

这个 Gem 有

@1c7 就是想从外部获取 passive 方法中的某个关键字,比如上边

m << { :version=>@body.scan(/<p>Version ([\d\.]+)<\/p>/)[0][0] }

这行中的关键字“version”

@teddyinfi 谢谢大哥,🇻

@IChou

root@localhost:/home# gem install method_source

命令执行后,确实可以了

@teddyinfi @IChou 昨天工作着急下班,刚才测试, 注意到

self.method(:passive).source.display               # 能输出passive方法的所有东西
self.method(:passive).source.display.class         #输出跟上边完全一样
p self.method(:passive).source.display.class       #输出为NilClass

虽然有输出,但是并不能引用。(已经在开头 require 'method_source')

引用?

puts self.method(:passive).source.to_s

@rubyist518

method(:passive).source  #可以输出,而且是String类型。不过需要在开头require 'method_source'
instance_method(:passive).source #提示报错,undefined method `instance_method' for main:Object (NoMethodError),还没搞明白。。

@teddyinfi 这样就跟 method(:passive).source 一样了,不用 to_s 了 哈哈,谢谢了

self.method(:passive).source == method(:passive).source  #=> true

兄跌,你对 instance_method 有点误解啊

# Rc135xx 是一个类
class Rc135xx
  # passive 是个实例方法,必须在类的实例上才能调用
  def passive
    p self.object_id
  end
end
# 再来个 r ,就是一个实例
r =  Rc135xx.new


Rc135xx.method(:passive) #=> NameError: undefined method `passive' for class `#<Class:Rc135xx>'

Rc135xx.instance_method(:passive) #=> #<UnboundMethod: Rc135xx#passive>

r.instance_method(:passive) #=> NoMethodError: undefined method `instance_method' for #<Rc135xx:0x007fcc2296ed80>

r.method(:passive) #=> #<Method: Rc135xx#passive>

举个栗子

  • Rc135xx => 成都二孃鸡爪爪公司
  • r => 二孃鸡爪爪的一个分店
Rc135xx.method(:passive) # 你去二孃鸡爪爪总公司买鸡爪爪,汪汪汪?你买个锤子,这里是办公室
Rc135xx.instance_method(:passive) # 你问二孃鸡爪爪:你们下面的门店可以买鸡爪爪哇? 可以,我给你指路嘛

r.method(:passive) # 你到二孃鸡爪爪门店买鸡爪爪,莫得问题
r.instance_method(:passive) # 你问门店你们的员工可以私自出去卖鸡爪爪么?……你大概会被打哟

你的代码里面调用的 self,就是类的一个实例,等同于 r

You need to Sign in before reply, if you don't have an account, please Sign up first.