Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. There'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:
  5. irb> str = "rlabuonora@yahoo.com"
  6. => "rlabuonora@yahoo.com"
  7. irb> str.methods
  8. => [:<=>, :==, :===, :eql?, :hash, :casecmp, :+, :*, :%, :[], :[]=,
  9. ...
  10.  
  11.  
  12. # str[] is syntactic sugar for str.[]=
  13. irb> str.[]=(0, "s")
  14. => "s"
  15. irb> str
  16. => "slabuonora@yahoo.com"
  17.  
  18. # you can user integers in []
  19. irb> str[0] = "r"
  20. => "r"
  21. irb> str
  22. => "rlabuonora@yahoo.com"
  23. irb>
  24.  
  25. # but also ranges
  26. irb> str[(0..10)] = "*" * 10
  27. => "**********"
  28. irb> str
  29. => "**********yahoo.com"
  30.  
  31. # and even regular expressions
  32. irb> str = "rlabuonora@yahoo.com"
  33. => "rlabuonora@yahoo.com"
  34. irb> str[/@(.*).com/] = "@gmail.com"
  35. => "@gmail.com"
  36. irb> str
  37. => "rlabuonora@gmail.com"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement