Forum: Ruby Noob question: while or for loop within a list

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1742c7dbb6d313e951cd78acf817febe?d=identicon&s=25 Darin Ginther (dginther)
on 2009-02-02 20:14
Essentially I want to do the following:
list = "url1, irl2, url3, url4, url5"
while [ list ];  #in other words, we loop 5 times or = # things in list
   do
   #A Bunch of Stuff 1
   #A Bunch of Stuff 2
done



My list is essentially a list of URLs.  I'm most familiar with doing it
in a text list, but I guess I could create an array, count that array,
and then do a loop with a counter.


Suggestions / Best practices?
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-02-02 20:19
(Received via mailing list)
On [Tue, 03.02.2009 04:14], Darin Ginther wrote:
> Essentially I want to do the following:
> list = "url1, irl2, url3, url4, url5"
> while [ list ];  #in other words, we loop 5 times or = # things in list
>    do
>    #A Bunch of Stuff 1
>    #A Bunch of Stuff 2
> done
>
[snipp]
> Suggestions / Best practices?

list.split(", ").each do |url|
  # do fancy stuff with our url
end
1742c7dbb6d313e951cd78acf817febe?d=identicon&s=25 Darin Ginther (dginther)
on 2009-02-02 20:27
 How do I access the individual element of the split?
IE:

For $i in [list];
  do
    #A Bunch of Stuff 1 + $i
    #A Bunch of Stuff 2 + $i
  done
exit
47aff267a58c012d222fd4d74f6beb54?d=identicon&s=25 Dominik Honnef (Guest)
on 2009-02-02 20:31
(Received via mailing list)
My code does exactly what you want.

.each is iterating over each item and stores it in the variable "url".
So instead of $i you got url.

And you should read some basic stuff about Ruby, like the Pickaxe, to
get started.
1742c7dbb6d313e951cd78acf817febe?d=identicon&s=25 Darin Ginther (dginther)
on 2009-02-02 20:40
list = "A,B,C,D"
list.split(", ").each do |url|
   puts "URL IS:"+url
end

Output: URL IS:A,B,C,D



Is using an array a "more correct" way to do it in Ruby?
D7908f05c89e965f6bc5308ad6f41256?d=identicon&s=25 Siep Korteling (steenslag)
on 2009-02-02 20:48
Darin Ginther wrote:
>
> list = "A,B,C,D"
> list.split(", ").each do |url|
>    puts "URL IS:"+url
> end
>
> Output: URL IS:A,B,C,D
>
>
>
> Is using an array a "more correct" way to do it in Ruby?

Your first example began with

list = "url1, irl2, url3, url4, url5"

but now your list looks like this:

list = "A,B,C,D"

The spaces are missing, so splitting on a comma followed by a space does
not split anything in your second example. In your first example, it
produces an array with 4 elements. In your second list it produces an
array with one elemement: "A,B,C,D"

hth,

Siep
C1b6b5557723c9db912b075e954166d3?d=identicon&s=25 Jeff Moore (djief)
on 2009-02-02 20:55
Darin Ginther wrote:
>
> list = "A,B,C,D"
> list.split(", ").each do |url|
>    puts "URL IS:"+url
> end
>
> Output: URL IS:A,B,C,D
>
>
>
> Is using an array a "more correct" way to do it in Ruby?

The easy way...

l = %w{ www.yahoo.com www.google.com www.ruby-lang.com }

l.each { |e| puts e }

www.yahoo.com
www.google.com
www.ruby-lang.com
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-02-02 21:01
(Received via mailing list)
On 02.02.2009 20:40, Darin Ginther wrote:
> list = "A,B,C,D"
> list.split(", ").each do |url|
>    puts "URL IS:"+url
> end
>
> Output: URL IS:A,B,C,D
>
>
>
> Is using an array a "more correct" way to do it in Ruby?

Yes.  You can use the word modifier for easier typing:

urls = %w{http://foo.bar http://www.google.gov}

Now you can do

puts urls

urls.each do |url|
   puts "URL is #{url}"
end

If you want to do URL manipulations check out class URI.
http://www.ruby-doc.org/core/classes/URI.html

Kind regards

  robert
1742c7dbb6d313e951cd78acf817febe?d=identicon&s=25 Darin Ginther (dginther)
on 2009-02-02 21:06
Thank you guys.
Obviously I need to be more aware of my white space in Ruby. I've put
the two examples below - both one via a list and one via an array.

# List
list = "url1, url2, url3, url4, url5"
list.split(", ").each do |url|
  puts url
end


#Array
urls = %w{http://test1.com http://test2.com}
urls.each do |url|
   puts "URL is #{url}"
end
391f9b787cdc12aa2c179713f5103e3a?d=identicon&s=25 Ilan Berci (iberci)
on 2009-02-02 21:26
Darin,

Don't let white space get you down..
list.split(/\s*,\s*/).each do

ilan


Darin Ginther wrote:
> Thank you guys.
> Obviously I need to be more aware of my white space in Ruby. I've put
> the two examples below - both one via a list and one via an array.
>
> # List
> list = "url1, url2, url3, url4, url5"
> list.split(", ").each do |url|
>   puts url
> end
>
>
> #Array
> urls = %w{http://test1.com http://test2.com}
> urls.each do |url|
>    puts "URL is #{url}"
> end
This topic is locked and can not be replied to.