2

I would like to know if there is any elegant way to implement a loop for the following method. I can only come up with a regular while loop (Java programmer) as the following pseudo code:

while x<10       
  search = Google::Search::Web.new()
  search.query = "china"
  search.start = x
end

Someone knows a better way?

4
  • 1
    There is something amiss with this psedocode. You never change x anywhere. So if x < 10 at the loop start, the loop will run infinitely. If x >= 10 at the loop start, it will not run at all. Commented Jan 11, 2011 at 18:05
  • Google::Search::Web does not seem to have a start attribute. github.com/visionmedia/google-search/blob/master/lib/… Commented Jan 11, 2011 at 18:17
  • I customized the library. I should have added a statement that changes the value of X. Thank you all! Commented Jan 11, 2011 at 18:36
  • It really depends on what you want to do. As a straight loop, while is not used in Ruby as much as a loop with discrete start and end points. If you are conditionally incrementing your x value, then a while loop is fine because you don't have a fixed number of loops you have to run. Instead you'd be looping until 10 occurrences of that condition have occurred. Most of the time we want a fixed number of loops, but sometimes we want a fixed number of conditions, and only you can determine that. Commented Jan 11, 2011 at 19:04

4 Answers 4

11

Many alternatives:

# Will go 0..9
10.times do |i|
  search = Google::Search::Web.new()
  search.query = "china"
  search.start = i
end

# Will go 1..10
1.upto(10) do |i|
  search = Google::Search::Web.new()
  search.query = "china"
  search.start = i
end

# Will go 1..10
(1..10).each do |i|
  search = Google::Search::Web.new()
  search.query = "china"
  search.start = i
end
Sign up to request clarification or add additional context in comments.

1 Comment

4

Do you mean to do something like this?

(1..9).each do |i|
    search = Google::Search::Web.new()
    search.query = "china"
    search.start = i
end

That will run the query with start at 1, then start at 2, all the way up to start at 9. The 1..9 syntax is a range, inclusive on both sides.

UPDATE: The (1..9).each is probably the most idiomatic way to do this in ruby, but Jonas Elfström posted a cool link that quickly demonstrates some alternatives:

http://alicebobandmallory.com/articles/2010/06/21/a-simple-loop

Comments

0
10.times do |i|
    search = Google::Search::Web.new()
    search.query = "china"
    search.start = x
loop

Integers have the times method that takes a block and loops n times where n is the number.

Comments

0

Since it doesn't look like the value of x never actually changes, there are two possible ways to rewrite this. Either x >= 10 before the loop starts, then it will never be run, and it can simply be replaced with nothing. Otherwise, it's an infinite loop, and the most idiomatic way to write that is

loop do
  search = Google::Search::Web.new
  search.query = 'china'
  search.start = x
end

If you don't know the value of x beforehand, you can simply make the loop conditional:

loop do
  search = Google::Search::Web.new
  search.query = 'china'
  search.start = x
end if x < 10

If x is a message send whose method changes its return value, then I don't see any obvious way to improve your code other than removing the superfluous braces.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.