首 页 » 软件使用 » 轻松查看ruby类或对象或模块的方法,常量等

轻松查看ruby类或对象或模块的方法,常量等

[ 2014-04-24 22:44 | 浏览次数: 2,194次 | 1条评论 ]

写ruby脚本时经常会忘记某个函数名怎么写, 一般这时候会打开一个终端,再进入ruby的终端irb或pry,然后在终端中输入一些字符后按tab键自动补全,通过这种方法来查看某个方法的全名等.

那有没有更方便的方法呢,输入public_methods也是一种很好的方法,这样可以查看某个类有哪些方法,但它输出也太多了吧,我们其实想知道的是这个类特有的方法属性,但它输出的包含了所有父类的方法和属性.

那能不能只输出类自己的方法呢,当然可以,只要将类的public_methods减去父类的public_methods即可.

为了更方便,我特地写了一个方法,适合用于每个对象,类,模块.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
def get_class_info(cl)
	my_public_methods = cl.public_methods
	my_protected_methods = cl.protected_methods
	my_private_methods = cl.private_methods
 
	my_variables = cl.instance_variables
	my_constants = cl.constants
	my_class_variables = cl.class_variables
 
	my_instance_public_methods = cl.public_instance_methods
	my_instance_protected_methods = cl.protected_instance_methods
	my_instance_private_methods = cl.private_instance_methods
	if cl.class == Class
		parent = cl.superclass
	else
		parent = Module
	end
	if parent
		cl = parent
		my_public_methods -= cl.public_methods
		my_protected_methods -= cl.protected_methods
		my_private_methods -= cl.private_methods
 
		my_variables -= cl.instance_variables
		my_constants -= cl.constants
		my_class_variables -= cl.class_variables
 
		my_instance_public_methods -= cl.public_instance_methods
		my_instance_protected_methods -= cl.protected_instance_methods
		my_instance_private_methods -= cl.private_instance_methods
	end
 
 
	return my_public_methods,my_protected_methods,my_private_methods,my_variables,my_constants,my_class_variables,my_instance_public_methods,my_instance_protected_methods,my_instance_private_methods
end
 
class Object
	#打印自己定义的函数
	def self_methods
		#是否是实例
		is_instance = nil
		str = ""
		if self.class == Class
			my_public_methods = self.public_methods
			my_protect_methods = self.protected_methods
			my_private_methods = self.private_methods
			parent = self.superclass
		elsif self.class == Module
			str = "unsupport\n"
		else
			#实例
			is_instance = true
			my_public_methods = self.public_methods
			parent = self.class.superclass
		end
 
		if is_instance && parent
			parent_public_methods = parent.public_instance_methods
		elsif parent
			parent_public_methods = parent.public_methods
			parent_protected_methods = parent.protected_methods
			parent_private_methods = parent.private_methods
		end
 
		if my_public_methods
			my_public_methods -= parent_public_methods
		end
		if my_protect_methods
			my_protect_methods -= parent_protected_methods
		end
		if my_private_methods
			my_private_methods -= parent_private_methods
		end
 
 
		if my_public_methods && my_public_methods.size >0
			str += "-Public Methods-\n#{my_public_methods.sort}\n"
		end
		if my_protect_methods && my_protect_methods.size >0
			str+= "-Protect Methods-\n#{my_protect_methods.sort}\n"
		end
		if my_private_methods && my_private_methods.size >0
			str+= "-Private Methods-\n#{my_private_methods.sort}\n"
		end
		#return str
		Kernel.puts str
	end
 
	#打印相关信息
	def info
		str = ""
		if self.class == Class || self.class==Module  #类
			my_public_methods,my_protected_methods,my_private_methods,my_variables,my_constants,my_class_variables,
			my_instance_public_methods,my_instance_protected_methods,my_instance_private_methods = get_class_info self
 
			if self.class == Class
				str += "Class: #{self.name}\n"
			else
				str += "Module: #{self.name}\n"
			end
			if self.class == Class
				if self.superclass
					str += "Inherit: #{self.ancestors[1..-1]}\n"
				end
			end
			if my_public_methods.size>0
				str += "Public methods:\n#{my_public_methods.sort}\n"
			end
			if my_protected_methods.size>0
				str += "Protect methods:\n#{my_protected_methods.sort}\n"
			end
			if self.class == Class
				puts self
				if my_private_methods.size>0
					str += "Private methods:\n#{my_private_methods.sort}\n"
				end
			end
			if my_variables.size>0
				str += "Variables:\n"
				my_variables.each_with_index do |var,index|
					value = instance_variable_get(var)
					if index != 0
						str += ", "
					end
					str += "#{var}=#{value}"
				end
				str += "\n"
			end
			if my_constants.size>0
				str += "Constants:\n"
				my_constants.each_with_index do |var,index|
					value = self.const_get(var)
					if index != 0
						str += ", "
					end
					str += "#{var}=#{value}"
				end
				str += "\n"
			end
			if my_class_variables.size>0
				str += "Class variables:\n"
				my_class_variables.each_with_index do |var,index|
					value = class_variable_get(var)
					if index != 0
						str += ", "
					end
					str += "#{var}=#{value}"
				end
				str += "\n"
			end
 
			if my_instance_public_methods.size>0
				str += "Instance Public methods:\n#{my_instance_public_methods.sort}\n"
			end
			if my_instance_protected_methods.size>0
				str += "Instance Protect methods:\n#{my_instance_protected_methods.sort}\n"
			end
			if self.class == Class
				if my_instance_private_methods.size>0
					str += "Instance Private methods:\n#{my_instance_private_methods.sort}\n"
				end
			end
		else
			#实例
			str += "Object: from class #{self.class}\n"
			if self.singleton_methods.size>0
				str += "Singleton methods:\n #{self.singleton_methods.sort}\n"
			end
			my_variables = self.instance_variables
			if my_variables.size>0
				str += "Variables:\n"
				my_variables.each_with_index do |var,index|
					value = instance_variable_get(var)
					if index != 0
						str += ", "
					end
					str += "#{var}=#{value}"
				end
				str += "\n"
			end
			my_public_methods = self.public_methods
			parent = self.class.superclass
			if parent
				my_public_methods -= parent.public_instance_methods
			end
			if my_public_methods.size>0
				str+= "Useful self methods:\n#{my_public_methods.sort}\n"
			end
 
		end
		#return str
		Kernel.puts str
	end
end

以上代码给object添加了方法info和self_methods,

其中self_methods可以打印自己的函数(不包含父类).

例:

[3] (main)> String.new().self_methods

-Public Methods-

[:%, :*, :+, :<, :<<, :<=, :>, :>=, :[], :[]=, :ascii_only?, :b, :between?, :byt

es, :bytesize, :byteslice, :capitalize, :capitalize!, :casecmp, :center, :chars,

:chomp, :chomp!, :chop, :chop!, :chr, :clear, :codepoints, :concat, :count, :cr

ypt, :delete, :delete!, :downcase, :downcase!, :dump, :each_byte, :each_char, :e

ach_codepoint, :each_line, :empty?, :encode, :encode!, :encoding, :end_with?, :f

orce_encoding, :getbyte, :gsub, :gsub!, :hex, :include?, :index, :insert, :inter

n, :length, :lines, :ljust, :lstrip, :lstrip!, :match, :next, :next!, :oct, :ord

, :partition, :prepend, :replace, :reverse, :reverse!, :rindex, :rjust, :rpartit

ion, :rstrip, :rstrip!, :scan, :setbyte, :shell_split, :shellescape, :shellsplit

, :size, :slice, :slice!, :split, :squeeze, :squeeze!, :start_with?, :strip, :st

rip!, :sub, :sub!, :succ, :succ!, :sum, :swapcase, :swapcase!, :to_c, :to_f, :to

_i, :to_r, :to_str, :to_sym, :tr, :tr!, :tr_s, :tr_s!, :unpack, :upcase, :upcase

!, :upto, :valid_encoding?]

更有用的函数info,这个可以查看类,对象,模块的相关信息.

例:

[4] pry(main)> Math.info

Module: Math

Public methods:

[:acos, :acosh, :asin, :asinh, :atan, :atan2, :atanh, :cbrt, :cos, :cosh, :erf,

:erfc, :exp, :frexp, :gamma, :hypot, :ldexp, :lgamma, :log, :log10, :log2, :sin,

:sinh, :sqrt, :tan, :tanh]

Constants:

DomainError=Math::DomainError, PI=3.141592653589793, E=2.718281828459045

以上代码放在pry的配置文件.pryrc里,以后就可以在打开pry时自动添加函数info和self_methods.

代码开源在http://git.oschina.net/hgyxbll/pryrc

您可以任意转载本文,但转载时请勿删除作者信息。
作者:hgyxbll,原文链接:http://shashanzhao.com/archives/1007.html
Tags:
上一篇: | 下一篇:

沙发已经被占,赶紧抢板凳吧![点此发言]

  1. 路过就应该要评论一下.