التابع format‎ الخاص بالصنف Kernel في روبي

من موسوعة حسوب
< Ruby‏ | Kernel
مراجعة 22:35، 21 أكتوبر 2018 بواسطة محمد-بغات (نقاش | مساهمات) (أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: التابع <code>format‎</code> الخاص بالصنف <code>Kernel</code> في روبي}}</noinclude> تصنيف: Ruby تصني...')
(فرق) → مراجعة أقدم | المراجعة الحالية (فرق) | مراجعة أحدث ← (فرق)
اذهب إلى التنقل اذهب إلى البحث

يعيد التابع السلسلة النصية الناتجة من تطبيق format_string على أي وسائط إضافية. داخل السلسلة النصية format_string، ستُنسخ كل الأحرف في النتيجة باستثناء تسلسلات التنسيق (format sequences). صياغة تسلسلات التنسيق هي كما يلي.

%[flags][width][.precision]type

تتألف تسلسلات التنسيق من علامة النسبة المئوية، متبوعة بثلاث حقول اختيارية، flags و width ، وprecision، ثم تنتهي بحرف نوع الحقل. يتحكم نوع الحقل في كيفية تأويل الوسيط المقابل sprintf، بينما تعدل الحقول flags ذلك التأويل. احرف نوع الحقل هي:

Field |  Integer Format
------+--------------------------------------------------------------
  b   | Convert argument as a binary number.
      | Negative numbers will be displayed as a two's complement
      | prefixed with `..1'.
  B   | Equivalent to `b', but uses an uppercase 0B for prefix
      | in the alternative format by #.
  d   | Convert argument as a decimal number.
  i   | Identical to `d'.
  o   | Convert argument as an octal number.
      | Negative numbers will be displayed as a two's complement
      | prefixed with `..7'.
  u   | Identical to `d'.
  x   | Convert argument as a hexadecimal number.
      | Negative numbers will be displayed as a two's complement
      | prefixed with `..f' (representing an infinite string of
      | leading 'ff's).
  X   | Equivalent to `x', but uses uppercase letters.
Field |  Float Format
------+--------------------------------------------------------------
  e   | Convert floating point argument into exponential notation
      | with one digit before the decimal point as [-]d.dddddde[+-]dd.
      | The precision specifies the number of digits after the decimal
      | point (defaulting to six).
  E   | Equivalent to `e', but uses an uppercase E to indicate
      | the exponent.
  f   | Convert floating point argument as [-]ddd.dddddd,
      | where the precision specifies the number of digits after
      | the decimal point.
  g   | Convert a floating point number using exponential form
      | if the exponent is less than -4 or greater than or
      | equal to the precision, or in dd.dddd form otherwise.
      | The precision specifies the number of significant digits.
  G   | Equivalent to `g', but use an uppercase `E' in exponent form.
  a   | Convert floating point argument as [-]0xh.hhhhp[+-]dd,
      | which is consisted from optional sign, "0x", fraction part
      | as hexadecimal, "p", and exponential part as decimal.
  A   | Equivalent to `a', but use uppercase `X' and `P'.
Field |  Other Format
------+--------------------------------------------------------------
  c   | Argument is the numeric code for a single character or
      | a single character string itself.
  p   | The valuing of argument.inspect.
  s   | Argument is a string to be substituted.  If the format
      | sequence contains a precision, at most that many characters
      | will be copied.
  %   | A percent sign itself will be displayed.  No argument taken.

تقوم الحقول flags بتعديل سلوك التنسيقات. أحرف الراية flag هي:

Flag     | Applies to    | Meaning
---------+---------------+-----------------------------------------
space    | bBdiouxX      | Leave a space at the start of
         | aAeEfgG       | non-negative numbers.
         | (numeric fmt) | For `o', `x', `X', `b' and `B', use
         |               | a minus sign with absolute value for
         |               | negative values.
---------+---------------+-----------------------------------------
(digit)$ | all           | Specifies the absolute argument number
         |               | for this field.  Absolute and relative
         |               | argument numbers cannot be mixed in a
         |               | sprintf string.
---------+---------------+-----------------------------------------
 #       | bBoxX         | Use an alternative format.
         | aAeEfgG       | For the conversions `o', increase the precision
         |               | until the first digit will be `0' if
         |               | it is not formatted as complements.
         |               | For the conversions `x', `X', `b' and `B'
         |               | on non-zero, prefix the result with ``0x'',
         |               | ``0X'', ``0b'' and ``0B'', respectively.
         |               | For `a', `A', `e', `E', `f', `g', and 'G',
         |               | force a decimal point to be added,
         |               | even if no digits follow.
         |               | For `g' and 'G', do not remove trailing zeros.
---------+---------------+-----------------------------------------
+        | bBdiouxX      | Add a leading plus sign to non-negative
         | aAeEfgG       | numbers.
         | (numeric fmt) | For `o', `x', `X', `b' and `B', use
         |               | a minus sign with absolute value for
         |               | negative values.
---------+---------------+-----------------------------------------
-        | all           | Left-justify the result of this conversion.
---------+---------------+-----------------------------------------
0 (zero) | bBdiouxX      | Pad with zeros, not spaces.
         | aAeEfgG       | For `o', `x', `X', `b' and `B', radix-1
         | (numeric fmt) | is used for negative numbers formatted as
         |               | complements.
---------+---------------+-----------------------------------------
*        | all           | Use the next argument as the field width.
         |               | If negative, left-justify the result. If the
         |               | asterisk is followed by a number and a dollar
         |               | sign, use the indicated argument as the width.‎

أمثلة على الرايات flags:

الحقل width هو عدد صحيح اختياري، متبوع اختياريًا بنقطة وحقل precision. يحدد width الحد الأدنى لعدد الأحرف التي ستُكتب في النتيجة الخاصة بهذا الحقل. أمثلة على الحقل width:

للحقول العددية، يتحكم الحقل precision في عدد المنازل العشرية المعروضة. بالنسبة للحقول النصية، يحدد الحقل precision الحد الأقصى لعدد الأحرف التي ستُنسخ من السلسلة. (وهكذا، فإن تسلسل التنسيق %10.10s سيخصص دائمًا عشرة أحرف بالضبط في النتيجة.) أمثلة على الحقول precision:

أمثلة:

بالنسبة للتنسيقات الأكثر تعقيدًا، تدعم روبي المراجع الاسمية (reference by name). حيث يستخدم النمط <name> s نمط التنسيق (format style)، وذلك على خلاف النمط٪ {name}. أمثلة:

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

format(format_string [, arguments...] ) string

المعاملات

format_string‎

arguments...‎

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

أمثلة

مثال على استخدام التابع format‎:

# `+' and space flag specifies the sign of non-negative numbers.
sprintf("%d", 123)  #=> "123"
sprintf("%+d", 123) #=> "+123"
sprintf("% d", 123) #=> " 123"
# `#' flag for `o' increases number of digits to show `0'.
# `+' and space flag changes format of negative numbers.
sprintf("%o", 123)   #=> "173"
sprintf("%#o", 123)  #=> "0173"
sprintf("%+o", -123) #=> "-173"
sprintf("%o", -123)  #=> "..7605"
sprintf("%#o", -123) #=> "..7605"
# `#' flag for `x' add a prefix `0x' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%x", 123)   #=> "7b"
sprintf("%#x", 123)  #=> "0x7b"
sprintf("%+x", -123) #=> "-7b"
sprintf("%x", -123)  #=> "..f85"
sprintf("%#x", -123) #=> "0x..f85"
sprintf("%#x", 0)    #=> "0"
# `#' for `X' uses the prefix `0X'.
sprintf("%X", 123)  #=> "7B"
sprintf("%#X", 123) #=> "0X7B"
# `#' flag for `b' add a prefix `0b' for non-zero numbers.
# `+' and space flag disables complements for negative numbers.
sprintf("%b", 123)   #=> "1111011"
sprintf("%#b", 123)  #=> "0b1111011"
sprintf("%+b", -123) #=> "-1111011"
sprintf("%b", -123)  #=> "..10000101"
sprintf("%#b", -123) #=> "0b..10000101"
sprintf("%#b", 0)    #=> "0"
# `#' for `B' uses the prefix `0B'.
sprintf("%B", 123)  #=> "1111011"
sprintf("%#B", 123) #=> "0B1111011"
# `#' for `e' forces to show the decimal point.
sprintf("%.0e", 1)  #=> "1e+00"
sprintf("%#.0e", 1) #=> "1.e+00"
# `#' for `f' forces to show the decimal point.
sprintf("%.0f", 1234)  #=> "1234"
sprintf("%#.0f", 1234) #=> "1234."
# `#' for `g' forces to show the decimal point.
# It also disables stripping lowest zeros.
sprintf("%g", 123.4)   #=> "123.4"
sprintf("%#g", 123.4)  #=> "123.400"
sprintf("%g", 123456)  #=> "123456"
sprintf("%#g", 123456) #=> "123456."

انظر أيضا

  • التابع fork: ينشئ التابع عملية فرعية (subprocess). في حال إعطاء كتلة، فستُنفّذ تلك الكتلة في العملية الفرعية، كما أن قيمة الحالة عند الانتهاء ستساوي صفر. وإلا ، فإن استدعاء التابع fork سيقوم بالإعادة مرتين، مرة في العملية الأم (parent, process)، حيث يعيد معرف العملية الفرعية، ومرة في العملية الفرعية، حيث يعيد nil. يمكن للعملية الفرعية الخروج باستخدام Kernel.exit! لتجنب تشغيل دوال at_exit. يجب أن تستخدم العملية الأم Process.wait لجمع حالات إنهاء عملياتها الفرعية، أو تستخدم Process.detach لتسجيل إهمال تلك الحالات؛ خلاف ذلك، قد يُراكم نظام التشغيل عمليات الزومبي (zombie processes).
  • التابع gets: يعيد التابع (ويُحيل على $_) السطر الموالي من قائمة الملفات في ARGV (أو $*)، أو من مجرى الإدخال القياسي في حالة عدم تمرير أي ملفات في سطر الأوامر. ويعيد nil في نهاية الملف. يحدد الوسيط الاختياري فاصل السجل. يتم تضمين الفاصل مع محتويات كل سجل. قي حال كان الوسيط sep يساوي nil فسيقرأ التابع gets كامل المحتوى، أما إذا كان الفاصل معدوم الطول (zero-length) فسيقرأ فقرة واحدة في كل مرة من المدخلات، بحيث تكون الفقرات مقسمة بواسطة سطرين جديدين متتاليين . إذا كان الوسيط الأول عددًا صحيحًا أو في حالإعطاء الوسيط الثاني الاختياري، فإنّ السلسلة النصية المعادة لن تكون أطول من القيمة المحددة من حيث عدد البتات (bytes). في حال تمرير عدة أسماء ملفات إلى ARGV ، فسيقرأ gets(nil) محتويات الملفات، واحدًا في كل مرة.

مصادر