Ruby Language
आत्मनिरीक्षण
खोज…
किसी वस्तु के तरीके देखें
किसी वस्तु का निरीक्षण करना
आप सार्वजनिक तरीकों को पा सकते हैं एक वस्तु या तो methods या public_methods विधियों का उपयोग करके प्रतिक्रिया कर सकती है, जो प्रतीकों की एक सरणी लौटाते हैं:
class Foo
def bar; 42; end
end
f = Foo.new
def f.yay; 17; end
p f.methods.sort
#=> [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :bar, :class, :clone,
#=> :define_singleton_method, :display, :dup, :enum_for, :eql?, :equal?, :extend,
#=> :freeze, :frozen?, :hash, :inspect, :instance_eval, :instance_exec,
#=> :instance_of?, :instance_variable_defined?, :instance_variable_get,
#=> :instance_variable_set, :instance_variables, :is_a?, :itself, :kind_of?,
#=> :method, :methods, :nil?, :object_id, :private_methods, :protected_methods,
#=> :public_method, :public_methods, :public_send, :remove_instance_variable,
#=> :respond_to?, :send, :singleton_class, :singleton_method, :singleton_methods,
#=> :taint, :tainted?, :tap, :to_enum, :to_s, :trust, :untaint, :untrust,
#=> :untrusted?, :yay]
अधिक लक्षित सूची के लिए, आप सभी वस्तुओं के लिए सामान्य तरीके निकाल सकते हैं, जैसे
p (f.methods - Object.methods).sort
#=> [:bar,:yay]
वैकल्पिक रूप से, आप false methods या public_methods पास कर सकते हैं:
p f.methods(false) # public and protected singleton methods of `f`
#=> [:yay]
p f.public_methods(false)
#=> [:yay, :bar]
आप एक वस्तु का उपयोग करने के लिए निजी और संरक्षित तरीकों पा सकते हैं private_methods और protected_methods :
p f.private_methods.sort
#=> [:Array, :Complex, :DelegateClass, :Float, :Hash, :Integer, :Rational, :String,
#=> :__callee__, :__dir__, :__method__, :`, :abort, :at_exit, :autoload, :autoload?,
#=> :binding, :block_given?, :caller, :caller_locations, :catch,
#=> :default_src_encoding, :eval, :exec, :exit, :exit!, :fail, :fork, :format, :gem,
#=> :gem_original_require, :gets, :global_variables, :initialize, :initialize_clone,
#=> :initialize_copy, :initialize_dup, :irb_binding, :iterator?, :lambda, :load,
#=> :local_variables, :loop, :method_missing, :open, :p, :print, :printf, :proc,
#=> :putc, :puts, :raise, :rand, :readline, :readlines, :require, :require_relative,
#=> :respond_to_missing?, :select, :set_trace_func, :singleton_method_added,
#=> :singleton_method_removed, :singleton_method_undefined, :sleep, :spawn,
#=> :sprintf, :srand, :syscall, :system, :test, :throw, :trace_var, :trap,
#=> :untrace_var, :warn]
p f.protected_methods
#=> []
साथ के रूप में methods और public_methods , आप पास कर सकते हैं false को private_methods और protected_methods विरासत में मिला तरीकों दूर ट्रिम करने के लिए।
एक कक्षा या मॉड्यूल का निरीक्षण
के अलावा methods , public_methods , protected_methods , और private_methods , वर्गों और मॉड्यूल का पर्दाफाश instance_methods , public_instance_methods , protected_instance_methods , और private_instance_methods वस्तुओं के लिए अवगत कराया तरीकों का निर्धारण करने के उस वर्ग या मॉड्यूल से प्राप्त करती हैं। उपरोक्त के रूप में, आप इन विधियों को विरासत में दिए गए तरीकों को बाहर करने के लिए false तरीके से पारित कर सकते हैं:
p Foo.instance_methods.sort
#=> [:!, :!=, :!~, :<=>, :==, :===, :=~, :__id__, :__send__, :bar, :class,
#=> :clone, :define_singleton_method, :display, :dup, :enum_for, :eql?,
#=> :equal?, :extend, :freeze, :frozen?, :hash, :inspect, :instance_eval,
#=> :instance_exec, :instance_of?, :instance_variable_defined?,
#=> :instance_variable_get, :instance_variable_set, :instance_variables,
#=> :is_a?, :itself, :kind_of?, :method, :methods, :nil?, :object_id,
#=> :private_methods, :protected_methods, :public_method, :public_methods,
#=> :public_send, :remove_instance_variable, :respond_to?, :send,
#=> :singleton_class, :singleton_method, :singleton_methods, :taint,
#=> :tainted?, :tap, :to_enum, :to_s, :trust, :untaint, :untrust, :untrusted?]
p Foo.instance_methods(false)
#=> [:bar]
अंत में, यदि आप भविष्य में इनमें से अधिकांश के नाम भूल जाते हैं, तो आप इन का उपयोग कर तरीकों के सभी पा सकते हैं methods :
p f.methods.grep(/methods/)
#=> [:private_methods, :methods, :protected_methods, :public_methods,
#=> :singleton_methods]
p Foo.methods.grep(/methods/)
#=> [:public_instance_methods, :instance_methods, :private_instance_methods,
#=> :protected_instance_methods, :private_methods, :methods,
#=> :protected_methods, :public_methods, :singleton_methods]
किसी ऑब्जेक्ट के इंस्टेंस चर देखें
यह का उपयोग कर अपने उदाहरण चर के बारे में एक वस्तु क्वेरी करने के लिए संभव है instance_variables , instance_variable_defined? , और instance_variable_get , और उन्हें प्रयोग को संशोधित instance_variable_set और remove_instance_variable :
class Foo
attr_reader :bar
def initialize
@bar = 42
end
end
f = Foo.new
f.instance_variables #=> [:@bar]
f.instance_variable_defined?(:@baz) #=> false
f.instance_variable_defined?(:@bar) #=> true
f.instance_variable_get(:@bar) #=> 42
f.instance_variable_set(:@bar, 17) #=> 17
f.bar #=> 17
f.remove_instance_variable(:@bar) #=> 17
f.bar #=> nil
f.instance_variables #=> []
उदाहरण चर के नामों में @ प्रतीक शामिल हैं। यदि आप इसे छोड़ देते हैं तो आपको एक त्रुटि मिलेगी:
f.instance_variable_defined?(:jim)
#=> NameError: `jim' is not allowed as an instance variable name
वैश्विक और स्थानीय चर देखें
Kernel global_variables और local_variables की सूची प्राप्त करने के तरीकों को उजागर करता है:
cats = 42
$demo = "in progress"
p global_variables.sort
#=> [:$!, :$", :$$, :$&, :$', :$*, :$+, :$,, :$-0, :$-F, :$-I, :$-K, :$-W, :$-a,
#=> :$-d, :$-i, :$-l, :$-p, :$-v, :$-w, :$., :$/, :$0, :$1, :$2, :$3, :$4, :$5,
#=> :$6, :$7, :$8, :$9, :$:, :$;, :$<, :$=, :$>, :$?, :$@, :$DEBUG, :$FILENAME,
#=> :$KCODE, :$LOADED_FEATURES, :$LOAD_PATH, :$PROGRAM_NAME, :$SAFE, :$VERBOSE,
#=> :$\, :$_, :$`, :$binding, :$demo, :$stderr, :$stdin, :$stdout, :$~]
p local_variables
#=> [:cats]
उदाहरण के चर के विपरीत, विशेष रूप से वैश्विक या स्थानीय चर प्राप्त करने, स्थापित करने या निकालने के लिए कोई विधियाँ नहीं हैं। ऐसी कार्यक्षमता की तलाश आमतौर पर एक संकेत है कि मूल्यों को संग्रहीत करने के लिए आपके कोड को एक हैश का उपयोग करने के लिए फिर से लिखना चाहिए। हालाँकि, यदि आपको नाम से वैश्विक या स्थानीय चर संशोधित करने हैं, तो आप स्ट्रिंग के साथ eval उपयोग कर सकते हैं:
var = "$demo"
eval(var) #=> "in progress"
eval("#{var} = 17")
p $demo #=> 17
डिफ़ॉल्ट रूप से, eval आपके दायरे का आपके वर्तमान दायरे में मूल्यांकन करेगा। एक अलग दायरे में स्थानीय चर का मूल्यांकन करने के लिए, आपको उस बंधन पर कब्जा करना होगा जहां स्थानीय चर मौजूद हैं।
def local_variable_get(name, bound=nil)
foo = :inside
eval(name,bound)
end
def test_1
foo = :outside
p local_variable_get("foo")
end
def test_2
foo = :outside
p local_variable_get("foo",binding)
end
test_1 #=> :inside
test_2 #=> :outside
इसके बाद के संस्करण में, test_1 करने के लिए एक बाध्यकारी में सफल नहीं हुआ local_variable_get , और इसलिए eval कि विधि है, जहां एक स्थानीय चर नामित के संदर्भ में मार डाला गया था foo के लिए स्थापित किया गया था :inside ।
देखें वर्ग चर
कक्षाएं और मॉड्यूल किसी अन्य वस्तु के रूप में उदाहरण चर का आत्मनिरीक्षण करने के लिए एक ही तरीके हैं। वर्ग चर और मॉड्यूल में वर्ग चर ( @@these_things ) को क्वेरी करने के लिए समान तरीके हैं:
p Module.methods.grep(/class_variable/)
#=> [:class_variables, :class_variable_get, :remove_class_variable,
#=> :class_variable_defined?, :class_variable_set]
class Foo
@@instances = 0
def initialize
@@instances += 1
end
end
class Bar < Foo; end
5.times{ Foo.new }
3.times{ Bar.new }
p Foo.class_variables #=> [:@@instances]
p Bar.class_variables #=> [:@@instances]
p Foo.class_variable_get(:@@instances) #=> 8
p Bar.class_variable_get(:@@instances) #=> 8
उदाहरण चर के समान, वर्ग चर का नाम @@ शुरू होना चाहिए, या आपको एक त्रुटि मिलेगी:
p Bar.class_variable_defined?( :instances )
#=> NameError: `instances' is not allowed as a class variable name