بكل بساطة هى القدرة على التعامل مع الmeta data بحيث تستطيع أن تعرف ماذا في المصدر و أي صف أوي طريقة يحتويها، و أي بارميتر تأخذها كل طريقة وهكذا...
هي مجرد كائن object عادى
class
هى طريقة تستخدم فى الحصول على نوع ال class الذى تم عمل ال object منه
#s is an object from what?
puts s.class
#output: String
class.name
تستخدم فى الحصول على إسم الclass
#what is the name of this class ?
puts s.class.name
#output: String
superclass
تستخدم فى الحصول على ال parent class لل class
#from what String was inherited?
puts String.superclass #object is the parent class of 'em all!
included_modules
للحصول على ال modules التي عمل لها احتوى include فى ال class
#the included modules in String
p String.included_modules
#output: [Enumerable, Comparable, Kernel]
p Object.included_modules
#output: [Kernel]
object_id
كل object ليه unique id وهذه الطريقة تستخدم فى الحصول على ذلك ال id
#each object has a unique id.
s1="Hi"
puts s1.object_id #6
s2="Hi"
puts s2.object_id #8
لاحظ التالى فى حال إن كائنين يشيروا لنفس ال object
s1="Hi"
s2=s1 #points to s1
puts s1.object_id #6
puts s2.object_id #6
constants
هى طريقة تعيد array فيها كل ال constants الموجودة
p Math.constants
#output: ["E", "PI"]
local_variables
تستخدم فى الحصول على ال local variables فى البرنامج
#get the local variables
p local_variables
#output: ["s", "s1", "s2"]
global_variables
تستخدم فى الحصول على ال 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", "$~", "$@", "$?", "$>",
# "$=", "$<", "$:", "$0", "$.", "$/", "$,", "$-n",
# "$*", "$SAFE", "$+", "$-p", "$&", "$'", "$$", "$-l"]
instance_variables
بتستخدم للحصول على ال instance_variables
p instance_variables # []
methods
تستخدم للحصول على ال 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?"]
instance_methods
تستخدم فى الحصول على ال methods الخاصة بال instance
#instance methods
p String.instance_methods
private_methods
تستخدم فى الحصول على ال private methods الموجودة!
#get the private methods.
p String.private_methods
public_methods
للحصول على ال public methods
protected_methods
للحصول على ال protected methods
singleton_methods
يستخدم فى الحصول على ال singleton methods
protected_methods
للحصول على ال protected methods
ونفس الشئ مع
[private|protected|public]_instance_methods
respond_to?(:methodName)
تستخدم فى معرفة هل يمكن إستخدام الطريقة methodName مع ال object ام لا
s="Hello"
puts s.respond_to?(:upcase) #true: can use upcase method
puts s.respond_to?(:keys) #false: can't use keys method
class hierarchy
هل تساءلت كيف تحدد بيئة التطويرIDEs الsuper classes الخاصة بصف معين مشتق منهم ؟ تابع المثال التالى ولن تحتار مرة أخرى !!
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