· 11 years ago · Jun 29, 2015, 05:43 AM
1There's a method that lets you substitute a bunch of characters for a corresponding set of characters
2(it's not gsub). Find that method and use it in a code sample.
3
4# Element assignment ([]=) is a method:
5irb> str = "rlabuonora@yahoo.com"
6=> "rlabuonora@yahoo.com"
7irb> str.methods
8=> [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=,
9 ...
10
11
12# str[] is syntactic sugar for str.[]=
13irb> str.[]=(0, "s")
14=> "s"
15irb> str
16=> "slabuonora@yahoo.com"
17
18# you can user integers in []
19irb> str[0] = "r"
20=> "r"
21irb> str
22=> "rlabuonora@yahoo.com"
23irb>
24
25# but also ranges
26irb> str[(0..10)] = "*" * 10
27=> "**********"
28irb> str
29=> "**********yahoo.com"
30
31# and even regular expressions
32irb> str = "rlabuonora@yahoo.com"
33=> "rlabuonora@yahoo.com"
34irb> str[/@(.*).com/] = "@gmail.com"
35=> "@gmail.com"
36irb> str
37=> "rlabuonora@gmail.com"