Open In App

Ruby | Symbol match function

Last Updated : 09 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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 :




# 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"


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 :




# 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"


Output :

Symbol a : geeks

Symbol b : åöó

Symbol c : GEEKS



Symbol a match form : 0

Symbol b match form : 

Symbol c match form : 1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads