Programming Freaks  | دورات ومقالات برمجيه

Please login or register.

Login with username, password and session length
Advanced search  

News:

Please Read our FAQ

Author Topic: Reflection  (Read 738 times)

Striky

  • Helping Freak
  • Administrator
  • Posting Freak
  • *****
  • Posts: 252
    • View Profile
    • WWW
    • Email
Reflection
« on: October 26, 2008, 05:38:07 PM »


بكل بساطة هى القدرة على التعامل مع الmeta data بحيث تستطيع أن تعرف ماذا في المصدر و أي صف أوي طريقة يحتويها، و أي بارميتر تأخذها كل طريقة وهكذا...
Code: [Select]
s="Hola!"

هي مجرد كائن object عادى

class
هى طريقة تستخدم فى الحصول على نوع ال class الذى تم عمل ال object منه
Code: [Select]
#s is an object from what?
puts s.class
#output: String


class.name
تستخدم فى الحصول على إسم الclass
Code: [Select]
#what is the name of this class ?
puts s.class.name
#output: String


superclass
تستخدم فى الحصول على ال parent class لل class
Code: [Select]
#from what String  was inherited?
puts String.superclass #object is the parent class of 'em all!


included_modules
للحصول على ال modules التي عمل لها احتوى include فى ال class
Code: [Select]
#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
Code: [Select]
#each object has a unique id.
s1="Hi"
puts s1.object_id  #6

s2="Hi"
puts s2.object_id  #8

لاحظ التالى فى حال إن كائنين يشيروا لنفس ال object
Code: [Select]
s1="Hi"
s2=s1 #points to s1

puts s1.object_id #6
puts s2.object_id #6



constants
هى طريقة تعيد array فيها كل ال constants الموجودة
Code: [Select]
p Math.constants
#output: ["E", "PI"]


local_variables
تستخدم فى الحصول على ال local variables فى البرنامج
Code: [Select]
#get the local variables
p local_variables
#output: ["s", "s1", "s2"]


global_variables
تستخدم فى الحصول على ال global variables
Code: [Select]
$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
Code: [Select]
p instance_variables # [] 


methods
تستخدم للحصول على ال methods الخاصة بال class
Code: [Select]
#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
Code: [Select]
#instance methods
p String.instance_methods


private_methods
تستخدم فى الحصول على ال private methods الموجودة!
Code: [Select]
#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 ام لا
Code: [Select]
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 الخاصة بصف معين مشتق منهم ؟ تابع المثال التالى ولن تحتار مرة أخرى !!
Code: [Select]
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


Logged

Life is just a chance to grow a soul. - A. Powell
Weblog: http://ahmedyoussef.wordpress.com/