Related Articles

Related Articles

Ruby | Symbol match function
  • Last Updated : 09 Dec, 2019

Symbol#match() : match() is a Symbol class method which matches the pattern with symbol.

Syntax: Symbol.match()

Parameter: Symbol values

Return: position – if pattern matches the Symbol otherwise return nil

Example #1 :



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Symbol.match() method
  
# declaring Symbol 
a = :aBcDeF
  
# declaring Symbol
b = :"\u{e4 f6 fc}"
  
# declaring Symbol
c = :ABCDEF
  
# Symbol 
puts "Symbol a : #{a}\n\n"
puts "Symbol b : #{b}\n\n"
puts "Symbol c : #{c}\n\n\n\n"
  
  
# match form 
puts "Symbol a match form : #{a.match(/D/)}\n\n"
puts "Symbol b match form : #{b.match(/o/)}\n\n"
puts "Symbol c match form : #{c.match(/C/)}\n\n"

chevron_right


Output :

Symbol a : aBcDeF

Symbol b : äöü

Symbol c : ABCDEF



Symbol a match form : 3

Symbol b match form : 

Symbol c match form : 2

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for Symbol.match() method
  
# declaring Symbol 
a = :geeks
  
# declaring Symbol
b = :"\u{e5 f6 f3}"
  
# declaring Symbol
c = :GEEKS
  
# Symbol 
puts "Symbol a : #{a}\n\n"
puts "Symbol b : #{b}\n\n"
puts "Symbol c : #{c}\n\n\n\n"
  
  
# match form 
puts "Symbol a match form : #{a.match(/g/)}\n\n"
puts "Symbol b match form : #{b.match(/a/)}\n\n"
puts "Symbol c match form : #{c.match(/EE/)}\n\n"

chevron_right


Output :

Symbol a : geeks

Symbol b : åöó

Symbol c : GEEKS



Symbol a match form : 0

Symbol b match form : 

Symbol c match form : 1



My Personal Notes arrow_drop_up
Recommended Articles
Page :