How to Convert One Character to Another on Ruby
Ruby comes with a built-in string class that contains numerous tools for performing common operations on strings of characters. For some operations, the "sub" function is useful, as it replaces all instances of one or more characters with another set.
Instructions
-
-
1
Click "Start," select "Run," then type "cmd" into the Run box to open the command line in Windows. Type "irb" into the command line to open the Ruby debugger.
-
2
Enter this line into the prompt to create a string:
a = "tea"
-
-
3
Replace all instances of the letter "a" with "e" in the string using this line:
a.sub!('a', 'e')
-
4
Use this line to see the result:
puts a
-
1
Tips & Warnings
The "sub" function in Ruby supports Regex commands.