Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old November 5th, 2006, 12:01 AM
AliasXNeo AliasXNeo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 292 AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level)AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level)AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 12 h 22 m 38 sec
Reputation Power: 3
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

Reply With Quote
  #2  
Old November 5th, 2006, 12:30 AM
SimonGreenhill's Avatar
SimonGreenhill SimonGreenhill is offline
(retired)
Dev Shed God 11th Plane (10000 - 10499 posts)
 
Join Date: Dec 2003
Location: The Laboratory
Posts: 10,101 SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)SimonGreenhill User rank is General 5th Grade (Above 100000 Reputation Level)  Folding Points: 4925 Folding Title: Novice Folder
Time spent in forums: 3 Months 3 Weeks 5 h 41 m 45 sec
Reputation Power: 1331
Facebook
Moved to the brand new "Ruby Programming" forum

Reply With Quote
  #3  
Old November 5th, 2006, 01:58 AM
mawe mawe is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 394 mawe User rank is Sergeant Major (2000 - 5000 Reputation Level)mawe User rank is Sergeant Major (2000 - 5000 Reputation Level)mawe User rank is Sergeant Major (2000 - 5000 Reputation Level)mawe User rank is Sergeant Major (2000 - 5000 Reputation Level)mawe User rank is Sergeant Major (2000 - 5000 Reputation Level)mawe User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 10 h 6 m 47 sec
Reputation Power: 45
Hi!

2 solutions:
Ruby Code:
Original - Ruby Code
    line = "Perl" newline = line.sub(/Perl/, "Ruby") # or line.sub!(/Perl/, "Ruby")

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
Comments on this post
AliasXNeo agrees: Thanks for the nice help! ~ AliasXNeo

Reply With Quote
  #4  
Old November 5th, 2006, 02:29 AM
AliasXNeo AliasXNeo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 292 AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level)AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level)AliasXNeo User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 12 h 22 m 38 sec
Reputation Power: 3
Wow, cleared it up perfectly. Thanks a lot!

Cheers,
~ Alias

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Regular Expressions In Ruby?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway