Reflection
بكل بساطة هى امكانية التعامل مع الميتاداتا الخاصة بالبرنامج ومكوناته .. تقدر تخلى روبى تحكيلك على كل اسرارها واعمق افكارها lol
الناس اللى بتبرمج IDEs مش آلهة بالعكس المعظم بيستخدم ال reflection!
كود:
s="Hola!"
class
هى ميثود بتستخدم فى الحصول على نوع ال class الذى تم عمل ال object منه
كود:
#s is an object from what? puts s.class #output: String
بتستخدم فى الحصول على إسم الclass
كود:
#what is the name of this class ? puts s.class.name #output: String
بتستخدم فى الحصول على ال parent class لل class
كود:
#from what String was inherited? puts String.superclass #object is the parent class of 'em all!
للحصول على ال modules اللى معمول ليها include فى ال class
كود:
#the included modules in String p String.included_modules #output: [Enumerable, Comparable, Kernel] p Object.included_modules #output: [Kernel]
كل object ليه unique id وهذه الميثود بتستخدم فى الحصول على ذلك ال id
كود:
#each object has a unique id. s1="Hi" puts s1.object_id #6 s2="Hi" puts s2.object_id #8
كود:
s1="Hi" s2=s1 #points to s1 puts s1.object_id #6 puts s2.object_id #6
هى ميثود بتعيد array فيها ال constants الموجودة
كود:
p Math.constants #output: ["E", "PI"]
بتستخدم فى الحصول على ال local variables فى البرنامج
كود:
#get the local variables p local_variables #output: ["s", "s1", "s2"]
بتستخدم فى الحصول على ال global variables
كود:
$globString="What's up????" $anotherGlobString="CRUEL, WORLD!" #gets the global variables p global_variables #output #["$FILENAME", "$LOADED_FEATURES", "$anotherGlobString", # "$VERBOSE", "$globString", "$PROGRAM_NAME", "$LOAD_PATH", # "$DEBUG", "$stdin", "$KCODE", "$stderr", "$stdout", "$defout", # "$deferr", "$-I", "$`", "$-K", "$\\", "$_", "$!", # "$\"", "$-a", "$-d", "$~", "[email protected]", "$?", "$>", # "$=", "$<", "$:", "$0", "$.", "$/", "$,", "$-n", # "$*", "$SAFE", "$+", "$-p", "$&", "$'", "$$", "$-l"]
بتستخدم للحصول على ال instance_variables
كود:
p instance_variables # []
بتستخدم للحصول على ال methods الخاصة بال class
كود:
#get all of the methods in string methodsAry=String.methods p methodsAry #output: #["new", "superclass", "allocate", "inherited", "initialize_copy", ..................... # "class_eval", ">", "<", "private_class_method", , # "protected_methods", "nil?", "freeze", "is_a?", "eql?"]
بتستخدم فى الحصول على ال methods الخاصة بال instance
كود:
#instance methods p String.instance_methods
بتستخدم فى الحصول على ال private methods الموجودة!
كود:
#get the private methods. p String.private_methods
للحصول على ال public methods
protected_methods
للحصول على ال protected methods
singleton_methods
بيستخدم فى الحصول على ال singleton methods
protected_methods
للحصول على ال protected methods
ونفس الشئ مع
كود:
[private|protected|public]_instance_methods
كود:
respond_to?(:methodName)
كود:
s="Hello" puts s.respond_to?(:upcase) #true: can use upcase method puts s.respond_to?(:keys) #false: can't use keys method
عمرك تساءلت إزاى بتحد فى ال IDEs الsuper classes اللى class معين مشتق منهم ؟ تابع المثال التالى ومش هتحتار تانى :D
كود:
class First < String end class Second < First end class Third < Second end c=Third while c print(c) print(" < ") c=c.superclass end puts
#output: Third < Second < First < String < Object
تعليق