2

Using Sinatra, for whatever reason, the result if number > 1000, is NOT outputting to browser. It immediately shows "It took 0 tries to guess your number 10000." for example. So while the if statement works by NOT executing the until loop, it won't display the output to the browser. Hopefully I'm just doing something really silly.

SIMPLY: "Number must be under 1000." # This doesn't output to browser... WHY!?

Does NOT work...

Extra credit: What is the easiest way to store the last attempted guesses so that rand ONLY tries NEW values and compares once per number. e.g., Make /lottery/10 impossible to have more than 10 guesses.

Code Below

get '/lottery/:number' do
  i = 0
  guess = 0
  number = params[:number].to_i
  if number > 1000 then
    "Number must be under 1000." # This doesn't output to browser... WHY!?
  else
    until number == guess do
      guess = rand(number) + 1
      puts "Guess: #{guess} | Attempt: #{i}"
      i +=1
    end
  end
  "It took #{i} tries to guess your number #{number}."
end
David Cahill
  • 1,477
  • 1
  • 10
  • 12
  • No, it's: IF number is GREATER than 1000. Then "error" with the output "Number must be under 1000". – David Cahill Nov 02 '13 at 13:18
  • For the extra credits, maybe take a look at http://stackoverflow.com/questions/3482149/how-do-i-pick-randomly-from-an-array (starter: create array (1..number).to_a , then call sample() on it and remove the sampled item from array). – Felix Nov 02 '13 at 13:26

1 Answers1

4

The basic idea is that what your method returns is given to the browser (this is supersimplified and there are also other ways to send stuff back). In ruby a method returns the outcome of its last statement (except a call to return is issued beforehand).

Now, your get '/lottery/:number' do only returns stuff in its last line. You can either return "by hand", or you accumulate content to be given back.

get '/lottery/:number' do
  i = 0
  guess = 0
  number = params[:number].to_i
  if number > 1000 then
    # Return "by hand" (stuff below does not execute).
    return "Number must be under 1000."
  else
    until number == guess do
      guess = rand(number) + 1
      puts "Guess: #{guess} | Attempt: #{i}"
      i +=1
    end
  end
  "It took #{i} tries to guess your number #{number}"
end

or

get '/lottery/:number' do
  i = 0
  guess = 0
  content_to_send_back = ""
  number = params[:number].to_i
  if number > 1000 then
    # Will return that later:
    content_to_send_back = "Number must be under 1000."
  else
    until number == guess do
      guess = rand(number) + 1
      puts "Guess: #{guess} | Attempt: #{i}"
      content_to_send_back += "..#{i}"
      i +=1
    end
    content_to_send_back = "It took #{i} tries to guess your number #{number}"
  end
  # Ruby 'magic': actually it does 'return content_to_send_back':
  content_to_send_back
end

(Note that the get block is not and does not look like your typical method, but we can treat it just as if.)

Felix
  • 4,510
  • 2
  • 31
  • 46
  • You sir, rock. Thanks for such a clear cut and simple example of how to do this and how to push content from multiple statement! – David Cahill Nov 02 '13 at 13:26
  • I suppose I should open a separate question for the extra credit question on storing the last attempted rand results and to not retry them.. Thanks again for everything! – David Cahill Nov 02 '13 at 13:27
  • Interestingly enough... a Google search for "content_to_send_back" only produces this page lol. If you have any additional documentation on assembling groups of output, perhaps into an array, or if I had to iterate through multiple tables, etc using a similar method as above, the best practices on that. Don't trouble yourself too much though. I'm still learning sinatra. Though documentation isn't fantastic... – David Cahill Nov 02 '13 at 13:49
  • Well usually you use sinatra with some sort of templating engine (e.g. haml) if you create stuff to be viewed in browsers. This was just an example. content_to_send_back is the variable name i have chosen. – Felix Nov 02 '13 at 13:54
  • Durp. Here I thought it was a function (didn't notice the variable assignment, etc) :P. Sorry. Yes, I'm diving into using erb, etc. Things are starting to come together. TY again for the jumpstart. – David Cahill Nov 02 '13 at 14:04