Upcoming SlideShare
×

# gsub (MOTM 2009.09)

570 views

Published on

Ruby Method of the Month 2009.09. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
570
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Presented: September 21, 2009 to the Columbus Ruby Brigade.

• Anybody know which subway system that&amp;#x2019;s from? (next slide)
• Hong Kong MTR Logo (Mass Transit Railway)
• sub vs. gsub
• sub vs. gsub
• BANG!

• Important: Note that the puts returns nil, so result is shortened.
• Important: Note that the puts returns nil, so result is shortened.
• Reminder: anyone can get up and do an MOTM.
• ### gsub (MOTM 2009.09)

1. 1. gsub M e t h o d o f t h e M o n t h Kevin Munc @muncman
2. 2. Global SUBstitutions String#gsub(pattern, replacement) String#gsub(pattern) { |match| block }
3. 3. Global SUBstitutions String#gsub(pattern, replacement) String#gsub(pattern) { |match| block } The ‘pattern’ can be a String or a Regexp.
4. 4. seq = "0123456789" => "0123456789" seq.gsub('no match', 'anything') => "0123456789" seq.gsub('567', '___') => "01234___89"
5. 5. seq.gsub('567', '_') => "01234_89" seq.gsub('567', '.....') => "01234.....89" seq.gsub('567','') => "0123489"
6. 6. seq.gsub(/[5-9]/, 'g') => "01234ggggg"
7. 7. seq.gsub(/[5-9]/, 'g') => "01234ggggg" seq.sub(/[5-9]/, 'g') => "01234g6789"
8. 8. Bang! (like a gun shot in a ghetto) seq = "Matz Dave DHH Zed Why" => "Matz Dave DHH Zed Why"
9. 9. Bang! (like a gun shot in a ghetto) seq = "Matz Dave DHH Zed Why" => "Matz Dave DHH Zed Why" seq.gsub!('Zed ','') => "Matz Dave DHH Why" seq => "Matz Dave DHH Why"
10. 10. With Blocks
11. 11. With Blocks “0123456789”.gsub(/d/) do |digit| digit.to_i + 1 end => "12345678910"
12. 12. With Blocks “0123456789”.gsub(/d/) do |digit| digit.to_i + 1 end => "12345678910" “0123456789”.gsub(/d/) do |digit| digit.to_i * 2 end => "024681012141618"
13. 13. seq = "0123456789" => "0123456789"
14. 14. seq = "0123456789" => "0123456789" seq.gsub(/d/) do |digit| (digit.to_i * 2).to_s + '-' end => "0-2-4-6-8-10-12-14-16-18-"
15. 15. seq = "0123456789" => "0123456789"
16. 16. seq = "0123456789" => "0123456789" seq.gsub(/d/) { |digit| digit if (digit.to_i % 2 == 0) } => "02468"
17. 17. "COJUG".gsub(/[^C].*\$/, 'RB') => "CRB"
18. 18. "a1b2c3".gsub(/d/) do |x| puts "#{x} cheers for CRB!" end 1 cheers for CRB! 2 cheers for CRB! 3 cheers for CRB! => "abc"
19. 19. Questions? Answers? Hungry? Image Credits: http://www.ﬂickr.com/photos/terry_wha/39869806/ - http://www.ﬂickr.com/photos/bredgur/2740011432/ - http://travel.asianfanatics.net/images/3/35/MTR-Corporation-logo.png - http://www.asiaworld-expo.com/Images/en-US/ static/map/tran_mtrMap3_l.jpg - http://www.ﬂickr.com/photos/32912172@N00/3143351169/ - http://www.vagaemprego.com.br/wp-content/uploads/2009/08/subway_logo_large1.jpg - http://www.ﬂickr.com/photos/25035545@N04/2477315249/ - http://www.ﬂickr.com/photos/turtlemom_nancy/1393994488/ - http://www.ﬂickr.com/photos/ben_pollard/2203650937/