الفرق بين المراجعتين ل"Ruby/Location"
< Ruby
اذهب إلى التنقل
اذهب إلى البحث
جميل-بيلوني (نقاش | مساهمات) ط (مراجعة وتدقيق.) |
|||
سطر 1: | سطر 1: | ||
− | <noinclude>{{DISPLAYTITLE: | + | <noinclude>{{DISPLAYTITLE:الصنف <code>Location</code> في روبي}}</noinclude> |
[[تصنيف: Ruby]] | [[تصنيف: Ruby]] | ||
+ | [[تصنيف: Ruby Class]] | ||
[[تصنيف: Ruby Location]] | [[تصنيف: Ruby Location]] | ||
− | تمثّل كائنات الصنف | + | تمثّل كائنات الصنف <code>Location</code> إطارات المكدس (stack frames)، وتُنشأ بواسطة التابع <code>[[Ruby/Kernel/caller_locations|Kernel.caller_locations]]</code>. |
− | + | إليك مثلًا الشيفرة التالية: | |
<syntaxhighlight lang="ruby"># caller_locations.rb | <syntaxhighlight lang="ruby"># caller_locations.rb | ||
def a(skip) | def a(skip) | ||
سطر 19: | سطر 20: | ||
end</syntaxhighlight> | end</syntaxhighlight> | ||
تشغيل الأمر <code>ruby caller_locations.rb</code> سوف ينتج: | تشغيل الأمر <code>ruby caller_locations.rb</code> سوف ينتج: | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="text">caller_locations.rb:2:in `a' |
caller_locations.rb:5:in `b' | caller_locations.rb:5:in `b' | ||
caller_locations.rb:8:in `c'</syntaxhighlight> | caller_locations.rb:8:in `c'</syntaxhighlight> | ||
سطر 34: | سطر 35: | ||
end</syntaxhighlight> | end</syntaxhighlight> | ||
الآن، إن قمت بتنفيذ <code>ruby foo.rb</code>، فيجب أن ترى المخرجات التالية: | الآن، إن قمت بتنفيذ <code>ruby foo.rb</code>، فيجب أن ترى المخرجات التالية: | ||
− | <syntaxhighlight lang=" | + | <syntaxhighlight lang="text">init.rb:4:in `initialize' |
init.rb:8:in `new' | init.rb:8:in `new' | ||
init.rb:8:in `<main>'</syntaxhighlight> | init.rb:8:in `<main>'</syntaxhighlight> | ||
− | ==توابع النسخة العامة | + | ==توابع النسخة العامة== |
− | ===[[Ruby/Location/absolute_path | | + | ===[[Ruby/Location/absolute_path |<code>absolute_path</code>]]=== |
− | يُعيد | + | يُعيد مسار الملف الكامل للإطار. |
− | ===[[Ruby/Location/base_label | | + | ===[[Ruby/Location/base_label |<code>base_label</code>]]=== |
− | يعيد | + | يعيد التسمية الأساسية (base label) لهذا الإطار. |
− | ===[[Ruby/Location/inspect | | + | ===[[Ruby/Location/inspect |<code>inspect</code>]]=== |
− | يُعيد | + | يُعيد نفس نتيجة استدعاء <code>inspect</code> مع السلسلة النصية التي يعيدها التابع <code>to_str</code>. |
− | ===[[Ruby/Location/label | | + | ===[[Ruby/Location/label |<code>label</code>]]=== |
− | يعيد | + | يعيد تسمية هذا الإطار. |
− | ===[[Ruby/Location/lineno | | + | ===[[Ruby/Location/lineno |<code>lineno</code>]]=== |
− | يعيد | + | يعيد رقم سطر هذا الإطار. |
− | ===[[Ruby/Location/path | | + | ===[[Ruby/Location/path |<code>path</code>]]=== |
− | يُعيد اسم ملف هذا | + | يُعيد اسم ملف هذا الإطار. |
− | ===[[Ruby/Location/to_s | | + | ===[[Ruby/Location/to_s |<code>to_s</code>]]=== |
− | يُعيد | + | يُعيد [[Ruby/String|سلسلة نصية]] تمثل هذا الإطار على نمط التابع <code>[[Ruby/Kernel/caller|Kernel.caller]]</code>. |
==مصادر== | ==مصادر== | ||
− | *[http://ruby-doc.org/core-2.5.1/Thread/Backtrace/Location.html | + | *[http://ruby-doc.org/core-2.5.1/Thread/Backtrace/Location.html صفحة الصنف Location في توثيق روبي الرسمي.] |
المراجعة الحالية بتاريخ 09:22، 6 ديسمبر 2018
تمثّل كائنات الصنف Location
إطارات المكدس (stack frames)، وتُنشأ بواسطة التابع Kernel.caller_locations
.
إليك مثلًا الشيفرة التالية:
# caller_locations.rb
def a(skip)
caller_locations(skip)
end
def b(skip)
a(skip)
end
def c(skip)
b(skip)
end
c(0..2).map do |call|
puts call.to_s
end
تشغيل الأمر ruby caller_locations.rb
سوف ينتج:
caller_locations.rb:2:in `a'
caller_locations.rb:5:in `b'
caller_locations.rb:8:in `c'
إليك مثالًا آخر بنتيجة مختلفة بعض الشيء:
# foo.rb
class Foo
attr_accessor :locations
def initialize(skip)
@locations = caller_locations(skip)
end
end
Foo.new(0..2).locations.map do |call|
puts call.to_s
end
الآن، إن قمت بتنفيذ ruby foo.rb
، فيجب أن ترى المخرجات التالية:
init.rb:4:in `initialize'
init.rb:8:in `new'
init.rb:8:in `<main>'
توابع النسخة العامة
absolute_path
يُعيد مسار الملف الكامل للإطار.
base_label
يعيد التسمية الأساسية (base label) لهذا الإطار.
inspect
يُعيد نفس نتيجة استدعاء inspect
مع السلسلة النصية التي يعيدها التابع to_str
.
label
يعيد تسمية هذا الإطار.
lineno
يعيد رقم سطر هذا الإطار.
path
يُعيد اسم ملف هذا الإطار.
to_s
يُعيد سلسلة نصية تمثل هذا الإطار على نمط التابع Kernel.caller
.