|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Regular Expressions In Ruby?
I'm reading a book on Ruby, and I just started a little while ago. I have gotten some decent ground and they just introduced Regular Expressions, but for some reason I can't get them to work. Take this code for example:
Code:
line = "Perl" line.sub(/Perl/, 'Ruby') puts line When I run the code I still see "Perl" getting printed to the string when it should have replaced it with "Ruby". Also, when I do stuff like: Code:
line = "Yes I like Ruby"
if line =~ /Ruby/
puts line
end
Instead of printing the index of where the expression starts (like the book says) it just prints out the string again. Am I doing something wrong? Cheers, ~ Alias |
|
#2
|
||||
|
||||
|
Moved to the brand new "Ruby Programming" forum
![]()
__________________
|
|
#3
|
|||||
|
|||||
|
Hi!
2 solutions: Ruby Code:
line.sub() does not change the original line, line.sub! does. This is very common in Ruby, other examples are Code:
chomp --- chomp! chop --- chop! strip --- strip! ... Regards, mawe |
|
#4
|
|||
|
|||
|
Wow, cleared it up perfectly. Thanks a lot!
Cheers, ~ Alias |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Regular Expressions In Ruby? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|