|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
Concatenating array item with string
Hi All,
I have a (possibly) unique problem, I have an array of values that I would like to concatenate with a string. eg; servers=['server1','server2','server2'] path = "/some_path" I would like to be able to concatenate server[i] with path in order to create a complete path. I have tried server[i] + path, however ruby doesnt allow this at all. Can some one let me know if this is possible at all? Cheers - Karym6 |
|
#2
|
|||
|
|||
|
Code:
servers = ["server1", "server2", "server3"]
path = "/"
servers.each { |s| path << s }
# path => /server1server2server3
Or perhaps you mean.. Code:
servers = ["server1", "server2", "server3"]
path = "/"
servers.each { |s| path << s + "/" }
# path => /server1/server2/server3/
You can use += .. or << |
|
#3
|
|||
|
|||
|
Code:
irb(main):001:0> s = ['server1', 'server2', 'server3']
=> ["server1", "server2", "server3"]
irb(main):002:0> pth = "/path"
=> "/path"
irb(main):003:0> s.collect { |i| pth + "/" + i }.each { |i| puts i }
/path/server1
/path/server2
/path/server3
=> ["/path/server1", "/path/server2", "/path/server3"]
irb(main):004:0> s.collect { |i| i + pth }.each { |i| puts i }
server1/path
server2/path
server3/path
=> ["server1/path", "server2/path", "server3/path"]
irb(main):005:0>
I'm not sure what isnt working for you... Could you post a snippet of teh problem area so that we may see that it is not a syntax error?
__________________
-- I'll provide you with reference points; if they dont work, refer to something else. If you process text, this might make your life a little easier. |
|
#4
|
|||
|
|||
|
Oh, I was thinking he wanted to make a complete path from all of the values..
+ is the concatenation operator << is combined assignment and concatenation |
|
#5
|
|||
|
|||
|
Code:
puts "Checking to see if all sites have home pages"
puts "Now checking..."
servers = ['server1','server2','server3','server4','server5']
office_urls = CSV.read 'C:\\ReleaseScripts\\tests\\officeURLs.csv'
rpb_urls = CSV.read 'C:\\ReleaseScripts\\tests\\Burls.csv'
supermarket_urls = CSV.read 'C:\\ReleaseScripts\\tests\\marketURLs.csv'
businessclub_urls = CSV.read 'C:\\ReleaseScripts\\tests\\ClubURLs.csv'
office_urls.each {|path|
servers.each |s| path << s + "URL"
puts path
}
puts "============================================================"
currently, it doesnt work as you will see - I have been trying out some of the suggestions above. Ideally, I would like to have <server><path> printed on one line. Cheers - Karym6 |
|
#6
|
|||
|
|||
|
Guys, cracked it - coudnt have done it with out your guidance
![]() I acheived my aim with: Code:
office_urls.each {|path| servers.collect { |i| i + "#{path}" }.each { |i| puts i }}
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Concatenating array item with string |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|