الفرق بين المراجعتين ل"Ruby/Thread/index operator"

من موسوعة حسوب
< Ruby‏ | Thread
اذهب إلى التنقل اذهب إلى البحث
ط (مراجعة وتدقيق.)
 
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: التابع <code>[]‎</code> الخاص بالصنف <code>Thread</code> في روبي}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: المعامل <code>[]‎</code> الخاص بالصنف <code>Thread</code> في روبي}}</noinclude>
 
[[تصنيف: Ruby]]
 
[[تصنيف: Ruby]]
[[تصنيف: Ruby Method]]
+
[[تصنيف: Ruby Operator]]
 
[[تصنيف: Ruby Thread]]
 
[[تصنيف: Ruby Thread]]
مرجع الخاصيات (Attribute Reference) - يُعيد قيمة متغير محلي-[[Ruby/Fiber|الليف]] fiber-local variable ([[Ruby/Fiber|الليف]] الأصلي لل[[Ruby/Thread|مهمة الفرعية]] الحالية إن لم يكن المتغير موجودًا داخل [[Ruby/Fiber|ليف]]) ، باستخدام إما رمز أو [[Ruby/String|سلسلة نصية]]. إن لم يكن المتغير المعطى (انظر فقرة البنية العامة) موجودًا، فستعاد <code>nil</code>.<syntaxhighlight lang="ruby">[
+
يعدُّ المعامل <code>[]</code> مرجعًا للخاصيات (Attribute Reference)، إذ يُعيد قيمة متغير محلي [[Ruby/Fiber|الليف]] fiber) local variable، أو [[Ruby/Fiber|الليف]] الأصلي لل[[Ruby/Thread|مهمة الفرعية]] الحالية إن لم يكن المتغير موجودًا داخل [[Ruby/Fiber|ليف]]) باستخدام إما رمز أو [[Ruby/String|سلسلة نصية]]. إن لم يكن المتغير المعطى (انظر فقرة البنية العامة) موجودًا، فستعاد <code>nil</code>.<syntaxhighlight lang="ruby">[
 
   Thread.new { Thread.current["name"] = "A" },
 
   Thread.new { Thread.current["name"] = "A" },
 
   Thread.new { Thread.current[:name]  = "B" },
 
   Thread.new { Thread.current[:name]  = "B" },
سطر 12: سطر 12:
 
end‎</syntaxhighlight>هذا سوف ينتج:<syntaxhighlight lang="ruby">#<Thread:0x00000002a54220 dead>: A
 
end‎</syntaxhighlight>هذا سوف ينتج:<syntaxhighlight lang="ruby">#<Thread:0x00000002a54220 dead>: A
 
#<Thread:0x00000002a541a8 dead>: B
 
#<Thread:0x00000002a541a8 dead>: B
#<Thread:0x00000002a54130 dead>: C</syntaxhighlight>المعاملان ‎<code>[]‎</code> و ‎<code>[[Ruby/Thread/index operator-3D|[]=‎]]</code> ليسا محليا [[Ruby/Thread|المهمة الفرعية]] (thread-local)، ولكنهما محليا [[Ruby/Fiber|الليف]] (fiber-local). هذا الارتباك لم يظهر إلا بعد روبي 1.9، حيث ظهرت [[Ruby/Fiber|الألياف]] لأول مرة. اختارت روبي 1.9 أن تكون التوابع محلية-[[Ruby/Fiber|الليف]] لتوفير الاصطلاح التالي لأجل النطاق الديناميكي (dynamic scope).<syntaxhighlight lang="ruby">def meth(newvalue)
+
#<Thread:0x00000002a54130 dead>: C</syntaxhighlight>المعاملان ‎<code>[]‎</code> و ‎<code>[[Ruby/Thread/index operator-3D|[]=‎]]</code> ليسا محليَّا [[Ruby/Thread|المهمة الفرعية]] (thread-local)، ولكنهما محليا [[Ruby/Fiber|الليف]] (fiber-local). هذا الارتباك لم يظهر إلا بعد روبي 1.9، حيث ظهرت [[Ruby/Fiber|الألياف]] لأول مرة. اختارت روبي 1.9 أن تكون التوابع محلية [[Ruby/Fiber|الليف]] لتوفير الاصطلاح التالي لأجل النطاق الديناميكي (dynamic scope).<syntaxhighlight lang="ruby">def meth(newvalue)
 
   begin
 
   begin
 
     oldvalue = Thread.current[:name]
 
     oldvalue = Thread.current[:name]
سطر 20: سطر 20:
 
     Thread.current[:name] = oldvalue
 
     Thread.current[:name] = oldvalue
 
   end
 
   end
end</syntaxhighlight>قد لا يعمل هذا الاصطلاح كنطاق ديناميكي إن كانت التوابع محلية-[[Ruby/Thread|المهمة الفرعية]] ( thread-local)، وقامت كتلة معطاة بتبديل (switches) [[Ruby/Fiber|الليف]].<syntaxhighlight lang="ruby">f = Fiber.new {
+
end</syntaxhighlight>قد لا يعمل هذا الاصطلاح كنطاق ديناميكي إن كانت التوابع محلية [[Ruby/Thread|المهمة الفرعية]] (thread-local)، وقامت كتلة معطاة بتبديل (switches) [[Ruby/Fiber|الليف]].<syntaxhighlight lang="ruby">f = Fiber.new {
 
   meth(1) {
 
   meth(1) {
 
     Fiber.yield
 
     Fiber.yield
سطر 31: سطر 31:
 
p Thread.current[:name]
 
p Thread.current[:name]
 
#=> nil if fiber-local
 
#=> nil if fiber-local
#=> 2 if thread-local (The value 2 is leaked to outside of meth method.)</syntaxhighlight>للمتغيرات محلية-[[Ruby/Thread|المهمة الفرعية]]، يرجى الاطلاع على التابعين <code>[[Ruby/Thread/thread_variable_get|thread_variable_get]]</code> و <code>[[Ruby/Thread/thread_variable_set|thread_variable_set]]</code>.
+
#=> 2 if thread-local (The value 2 is leaked to outside of meth method.)</syntaxhighlight>للمتغيرات محلية [[Ruby/Thread|المهمة الفرعية]]، يرجى الاطلاع على التابعين <code>[[Ruby/Thread/thread_variable_get|thread_variable_get]]</code> و <code>[[Ruby/Thread/thread_variable_set|thread_variable_set]]</code>.
 
==البنية العامة==
 
==البنية العامة==
 
<syntaxhighlight lang="ruby">thr[sym]→ obj or nil‎</syntaxhighlight>
 
<syntaxhighlight lang="ruby">thr[sym]→ obj or nil‎</syntaxhighlight>
==القيمة المُعادة==
+
==القيمة المعادة==
تعاد قيمة متغير محلي-[[Ruby/Fiber|الليف]] fiber-local variable. وإن لم يكن المتغير  موجودًا، فستعاد <code>nil</code>.
+
تعاد قيمة متغير محلي [[Ruby/Fiber|الليف]] (fiber-local variable). وإن لم يكن المتغير  موجودًا، فستعاد القيمة <code>nil</code>.
  
==انظر أيضا==
+
==انظر أيضًا==
* التابع ‎<code>[[Ruby/Thread/index_operator-3D|[]=‎‎]]</code>: معامل تعيين الخاصيات (Attribute Assignment)  
+
* المعامل ‎<code>[[Ruby/Thread/index_operator-3D|[]=‎‎]]</code>: يعيّن عندما يُستدعى بالشكل <code>thr[sym] = obj</code> قيمة المتغير <code>sym</code> محلي [[Ruby/Fiber|الليف]] (fiber-local variable)، باستخدام إما الرمز أو [[Ruby/String|السلسلة النصية]] <code>obj</code> أو ينشئها.
 
==مصادر==
 
==مصادر==
*[http://ruby-doc.org/core-2.5.1/Thread.html#method-i-5B-5D <nowiki>قسم التابع [‎] في الصنف Thread‎ في توثيق روبي الرسمي.</nowiki>]
+
*[http://ruby-doc.org/core-2.5.1/Thread.html#method-i-5B-5D <nowiki>قسم المعامل [‎] في الصنف Thread‎ في توثيق روبي الرسمي.</nowiki>]

المراجعة الحالية بتاريخ 07:15، 6 ديسمبر 2018

يعدُّ المعامل [] مرجعًا للخاصيات (Attribute Reference)، إذ يُعيد قيمة متغير محلي الليف fiber) local variable، أو الليف الأصلي للمهمة الفرعية الحالية إن لم يكن المتغير موجودًا داخل ليف) باستخدام إما رمز أو سلسلة نصية. إن لم يكن المتغير المعطى (انظر فقرة البنية العامة) موجودًا، فستعاد nil.

[
  Thread.new { Thread.current["name"] = "A" },
  Thread.new { Thread.current[:name]  = "B" },
  Thread.new { Thread.current["name"] = "C" }
].each do |th|
  th.join
  puts "#{th.inspect}: #{th[:name]}"
end

هذا سوف ينتج:

#<Thread:0x00000002a54220 dead>: A
#<Thread:0x00000002a541a8 dead>: B
#<Thread:0x00000002a54130 dead>: C

المعاملان ‎[]‎ و ‎[]=‎ ليسا محليَّا المهمة الفرعية (thread-local)، ولكنهما محليا الليف (fiber-local). هذا الارتباك لم يظهر إلا بعد روبي 1.9، حيث ظهرت الألياف لأول مرة. اختارت روبي 1.9 أن تكون التوابع محلية الليف لتوفير الاصطلاح التالي لأجل النطاق الديناميكي (dynamic scope).

def meth(newvalue)
  begin
    oldvalue = Thread.current[:name]
    Thread.current[:name] = newvalue
    yield
  ensure
    Thread.current[:name] = oldvalue
  end
end

قد لا يعمل هذا الاصطلاح كنطاق ديناميكي إن كانت التوابع محلية المهمة الفرعية (thread-local)، وقامت كتلة معطاة بتبديل (switches) الليف.

f = Fiber.new {
  meth(1) {
    Fiber.yield
  }
}
meth(2) {
  f.resume
}
f.resume
p Thread.current[:name]
#=> nil if fiber-local
#=> 2 if thread-local (The value 2 is leaked to outside of meth method.)

للمتغيرات محلية المهمة الفرعية، يرجى الاطلاع على التابعين thread_variable_get و thread_variable_set.

البنية العامة

thr[sym] obj or nil

القيمة المعادة

تعاد قيمة متغير محلي الليف (fiber-local variable). وإن لم يكن المتغير موجودًا، فستعاد القيمة nil.

انظر أيضًا

  • المعامل ‎[]=‎‎: يعيّن عندما يُستدعى بالشكل thr[sym] = obj قيمة المتغير sym محلي الليف (fiber-local variable)، باستخدام إما الرمز أو السلسلة النصية obj أو ينشئها.

مصادر