Ruby |符号类

这件事的目的 符号类 表示Ruby解释器中的名称。它们通常是通过使用 :name literal 语法或使用 为了_sym 方法。在程序执行期间,无论名称的内容和含义如何,都会为给定的名称字符串创建类似的符号对象。

null

例子:

# Ruby program to illustrate
# Symbol objects
# context 3
module Geeks1
class Max
end
$a1 = :Max
end
# context 1
module Geeks2
Max = 1
$a2 = :Max
end
# context 2
def Max()
end
$a3 = :Max
puts $a1 .object_id
puts $a2 .object_id
puts $a3 .object_id


输出:

1675428
1675428
1675428

说明: 如果Max是context1中的常数、context2中的方法或context3中的类,那么 :Max 在所有给定的上下文中都是相同的对象。

类方法

所有_符号: 此方法返回Ruby符号表中当前存在的符号数组。

Symbol.all_symbols

例子:

# Ruby program to illustrate
# the use of all_symbol method
# Using all_symbol method
puts Symbol .all_symbols.size
puts Symbol .all_symbols[ 1 , 20 ]


输出:

3250
"
#
$
%
&
'
(
)
*
+
,
-
.
/
:
;
<
=
>
?

实例方法

  1. ID2名称: 此方法返回一个表示 符号 .
    sym.id2name

    例子:

    # Ruby program to illustrate
    # the use of id2name method
    # Using id2name method
    p :Geeks.id2name
    p : "Welcome to GeeksforGeeks Portal" .id2name

    
    

    输出:

    "Geeks"
    "Welcome to GeeksforGeeks Portal"
    
  2. 检查: 此方法返回 符号 以符号文字的形式。
    sym.inspect

    例子:

    # Ruby program to illustrate
    # the use of inspect method
    # Using inspect method
    p :geeks .inspect
    p : "welcome to geeksforgeeks portal" .inspect

    
    

    输出:

    ":geeks"
    ":"welcome to geeksforgeeks portal""
    
  3. 致我们: 此方法类似于符号#id2name。此方法返回对应的名称或字符串 符号 .
    sym.to_s

    例子:

    # Ruby program to illustrate
    # the use of to_s method
    # Using to_s method
    p :geeks .to_s
    p : "welcome to geeksforgeeks portal" .to_s

    
    

    输出:

    "geeks"
    "welcome to geeksforgeeks portal"
    
  4. <=> : 相比之下 符号 其他符号 调用_s后,如果 符号 不到 其他符号 ,如果 符号 等于 其他符号 ,或者如果 符号 大于 其他符号 .
    sym <=> other_sym 

    例子:

    # Ruby program to illustrate
    # use of <=>
    # Using <=>
    a= :geeks
    b = : "welcome to geeksforgeeks portal"
    puts a<=>b
    c= :geeks
    puts a<=>c
    puts b<=>a

    
    

    输出:

    -1
    0
    1
    
  5. == : 如果 符号 等于 obj ,否则返回false。
    sym== obj

    例子:

    # Ruby program to illustrate
    # use of ==
    # Using ==
    a= :geeks
    b = : "welcome to geeksforgeeks portal"
    puts a==b
    c= :geeks
    puts a==c

    
    

    输出:

    false
    true
    
  6. [] : 此方法返回sym的值。致_【】。
    sym[idx] --> char 
    sym[b, n] --> string
    
  7. 资本化: 这种方法类似于符号#to_s。
    sym.capitalize
  8. 案例CMP: 此方法是symbol<=$gt;的不区分大小写版本;。它将返回-1、0、1或零。它在A-Z/A-Z上工作,而不是在所有Unicode上。用这种方法 当两个符号的编码不兼容或 其他符号 不是一个符号。
    sym.casecmp(other)

    例子:

    # Ruby program to illustrate
    # use of casecmp method
    # Using casecmp method
    puts :GeeKs.casecmp( :geeks )
    puts :GeeKsGfg.casecmp( :geeksG )
    puts :GeeKsGfg.casecmp( :geeksGfgz )
    puts :GeeKsGfg.casecmp( 3 )

    
    

    输出:

    0
    1
    -1
    nil
  9. 唐卡斯: 此方法将大写字母转换为小写字母。
    sym.downcase

    例子:

    # Ruby program to illustrate
    # use of the downcase method
    # Using the downcase method
    puts : "WELCOME TO GEEKSFORGEEKS" .downcase

    
    

    输出:

    :"welcome to geeksforgeeks"
  10. 长度: 此方法返回给定的 符号 .
    sym.length

    例子:

    # Ruby program to illustrate
    # use of length method
    # Using length method
    puts :GeeKsGfg.length

    
    

    输出:

    8
  11. 切片: 这种方法类似于 符号#至# 。此方法为您提供 符号 .
    sym.slice(index)
    sym.slice(b, n)
    

    例子:

    # Ruby program to illustrate
    # use of slice method
    # Using slice method
    p :GeeKsGfg.slice( 3 )
    p :GeeKsGfg.slice( 6 )

    
    

    输出:

    "K"
    "f"
    
  12. swapcase: 此方法交换中出现的字符的大小写 符号 .换句话说,它将小写转换为大写,将大写转换为小写。
    sym.swapcase

    例子:

    # Ruby program to illustrate
    # use of the swapcase method
    # Using swapcase method
    p "WELcome TO geeksFORGEEKS" .swapcase

    
    

    输出:

    "welCOME to GEEKSforgeeks"
  13. 上例: 此方法将小写字符转换为大写字符。
    sym.upcase

    例子:

    # Ruby program to illustrate
    # use of the upcase method
    # Using upcase method
    p "welcome to geeksforgeeks" .upcase

    
    

    输出:

    "WELCOME TO GEEKSFORGEEKS"
  14. 到_proc: 此方法返回一个 过程 对象,该对象通过 符号 .
    sym.to_proc

    例子:

    # Ruby program to illustrate
    # use of to_proc method
    # Using to_proc method
    p ( 1 .. 5 ).collect(& :to_s )

    
    

    输出:

     ["1", "2", "3", "4", "5"]
  15. 为了_sym 此方法返回与对象对应的符号。这里sym已经是一个符号了,所以在本例中它返回它。
    sym.to_sym

参考: https://ruby-doc.org/core-2.5.0/Symbol.html#method-i-5B-5D

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享