Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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:
  #1  
Old June 26th, 2009, 03:43 PM
PhdMama PhdMama is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 5 PhdMama User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 5 sec
Reputation Power: 0
Concatenation problem

Newbie question. I have a text file with a bunch of urls that look, let's say, like this:
yahoo/Business_%26_Finance/Stocks_A/index.html

I need to add a "prefix" and "suffix" so was doing this:

datafile.readlines.each {|url|

companyURL = "http://".concat(url.to_s).concat("?off=1")
puts companyURL
}

But the result has a line break in it before the "suffix"

http://yahoo/Business_%26_Finance/Stocks_A/index.html
?off=1

How do I get it to concatenate into one line?
TIA

Reply With Quote
  #2  
Old June 26th, 2009, 07:41 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 989 L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level)L7Sqr User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
Send a message via AIM to L7Sqr
The operator/method you are looking for is chomp. See the following example:
Code:
irb(main):001:0> s = "yahoo/Business_%26_Finance/Stocks_A/index.html\n"
=> "yahoo/Business_%26_Finance/Stocks_A/index.html\n"
irb(main):002:0> "http://" + s.chomp + "?off=1"
=> "http://yahoo/Business_%26_Finance/Stocks_A/index.html?off=1"

It is due to that fact that when you read a line in from user or file you get the newline (\n) with it.
What makes this kind of tricky in ruby (and I find it to be particularly pesky) is that puts is nice enough to strip that newline for you if it is at the end of your string.
Code:
irb(main):003:0> s = "some new string\n"
=> "some new string\n"
irb(main):004:0> puts s
some new string
=> nil
irb(main):005:0> s = "some new string\n\n"
=> "some new string\n\n"
irb(main):006:0> puts s
some new string

=> nil
So if you try to debug this problem by tracer bullets (outputting to console) it magically disappears.
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

Reply With Quote
  #3  
Old June 26th, 2009, 09:45 PM
PhdMama PhdMama is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 5 PhdMama User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 49 m 5 sec
Reputation Power: 0
Thanks!
I just figured out that .strip was working also but I was still clueless as to why I wasn't seeing the problem. Now I get it!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Concatenation problem

Developer Shed Advertisers and Affiliates



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

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


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap