Ruby gets function won't accept empty line more than once - ruby

I'm trying to write a simple ruby function that can prompt the user for a value and if the user presses ENTER by itself, then a default value is used.
In the following example, the first call to the Prompt function can be handled by pressing ENTER by itself and the default value will be used. However, the second time I call Prompt and press ENTER, nothing happens, and it turns out I have to press some other character before ENTER to return from the 'gets' call.
There must be some way to flush the input buffer to avoid this problem. Anyone know what to do?
Thanks,
David
def BlankString(aString)
return (aString == nil) ||
(aString.strip.length == 0)
end
#Display a message and accept the input
def Prompt(aMessage, defaultReponse = "")
found = false
result = ""
showDefault = BlankString(defaultReponse) ? "" : "(#{defaultReponse})"
while not found
puts "#{aMessage}#{showDefault}"
result = gets.chomp
result.strip!
found = result.length > 0
if !found
then if !BlankString(showDefault)
then
result = defaultReponse
found = true
end
end
end
return result
end
foo = Prompt("Prompt>", "sdfsdf")
puts foo
foo = Prompt("Prompt>", "default")
puts foo

This isn't technically an answer, but it'll help you anyways: use Highline (http://highline.rubyforge.org/), it'll save you a lot of grief if you're making a command-line interactive interface like this

I tried your code (under Windows) and it seemed to work fine.
What OS are you using?

I also tried your code (under OSX) with ruby 1.8.6 and it worked fine:
:! ruby prompt.rb
Prompt>(sdfsdf)
sdfsdf
Prompt>(default)
default
What do you get when you run the following?
c = gets
b = gets
a = gets
p [ a, b, c ]
I just hit 'Enter' 3x and get
["\n", "\n", "\n"]
I'm guessing what's wrong is that you're entering an infinite loop in your while statement by not passing it a defaultResponse (in some code that you're actually runinng that's not in your example).

I have confirmed (by running the program outside of Komodo) that the problem is in fact solely happening inside Komodo. I thank everyone for the feedback and taking the time to do the independent test (which I hadn't though of) to help narrow down the problem.

Related

running ruby on terminal gives me a different output than leetcode

I'm practicing leetcode and checking other people's solutions for the same exercise on my terminal gives a totally different output than expected. If I run it on leetcode it says the answer is correct...
What might be causing this?
reversing the array of chars - ["h","e","l","l","o"] - like so:
def reverse_string(s)
len = s.length
(0...len/2).each { |i|
temp = s[i]
swap_index = len-i-1
s[i] = s[swap_index]
s[swap_index] = temp
}
end
p reverse_string([["h","e","l","l","o"]])
output in my terminal -- 0...2
output in leetcode is as expected - ["o","l","l","e","h"]
Why does this happen?
thanks
Ruby's p function will print the value you pass to it.
The value you are choosing to pass to it is the return value of reverse_string.
You should add a line to the end of reverse_string that specifies what you want to return from it. In this case the line can simply consist of s. You don't even need an explict return in this case.

What am I doing wrong with this simple If function in ruby

I have been trying to teach myself to code and have gravitated towards Ruby.
Upon working on if functions I have come across this problem where even if the user input is == to the answer variable it still comes back as false in the if function. It will not come back as true
I can get it to work if it is an Integer, but for some reason it always returns false when having a string. Tried to convert is as well but can not figure it out.
Thank you for any help.
puts("For each question select A, B, or C")
puts("What is the capital of Kentucky?")
puts()
puts("A. Louisville")
puts("B. Frankfort")
puts("C. Bardstown")
puts()
answer = String("B")
text = gets()
puts()
if text == answer
puts("correct")
else
puts("incorrect")
puts("The correct answer was " + answer + ".")
end
There is an additional method you can call when declaring the "text" variable that will solve this.
The method you used preserves a line break at the end when you press enter to submit an answer so it will never actually correspond to the answer. To remove the line break use the "chomp" method.
text = gets.chomp
Hope this helped. Good luck.

Python 2, raw_input in define function. Returning value

I decided to have a little bit fun with coding python 2 in a long while and decided to try make somewhat playable game of Go.
I quickly tumbled on this issue while trying to make function for the game, it seems like I'm not understanding how raw_input or functions work fundamentally. I cut the part of the code that is giving me the trouble here. When I try to run this, I can get to give inputs, but after that I get NameError: name 'crd_x' is not defined, in the stoneplacement line. The code does work without using the function, but when I try to clean it up like this, I get the said error.
What exactly does return crd give out and how I'm actually supposed to give out variables from def functions?
def checkplayerinput():
if player_on_turn == 0:
crd = raw_input("Place Black stone (X-Y): ").split("-")
elif player_on_turn == 1:
crd = raw_input("Place White stone (X-Y): ").split("-")
crd_x = int(crd[1])
crd_y = int(crd[0])
return crd_x, crd_y
def stoneplacement(crd_x, crd_y, player_on_turn):
if board[crd_x][crd_y] == "+" and player_on_turn == 0:
board[crd_x][crd_y] = "B"
elif board[crd_x][crd_y] == "+" and player_on_turn == 1:
board[crd_x][crd_y] = "W"
stop = 0
while stop == 0:
#User input and derive coordinates
checkplayerinput()
# Stone placement
stoneplacement(crd_x, crd_y, player_on_turn)
edit. just switched the place of crd_x and crd_y like they are in the actual code. It produces exact same NameError however.
So crd is going to give you an array of strings based on whatever the user types in, separated by the ‘-‘ symbols. For example if player_on_turn == 0, and you entered for example “X-Y” (whatever your game calls for), checkplayerinput() would return crd as [“X”, “Y”].
Also, remember that an array is indexed starting at 0. So in this example, crd[0] == “X”.
Running through your code, does that explain why your stoneplacement() function throws a NameError? I.e. are you inputting the right raw_input to yield crd[1]?
Also, did you define crd before def checkplayerinput() in the code? According to python scope rules, if you define the variable crd inside of the function, it will not have a global value outside of it (and will throw an error if you reference it in another function). This is likely why you are able to run your code without the function definitions. Try adding a line “global crd” or “crd = [ ]” somewhere outside of the function and it will allow you to access crd in the stoneplacement(( function.

Why is the print() function not printing what I ask it to in Windows?

Just for fun, I'm building a very simple text-based game in Python 3.2. My development machine is running Windows 7, and I'm using the PyScripter IDE to develop this game.
I'm running into a slight problem with the print() function not printing a string out correctly. When you begin the game, the program displays a small menu with 3 choices: Login, Register, and Exit. When I go to register, the program creates an object from a custom class I made called Character. The character takes in a name variable, and then prints out a small message using the name variable. Here's the code that I have to do that:
user_choice = int(input("Please select a command: "))
if user_choice == 1:
print("Sorry, this function isn't currently implemented.",
"Please check back later.\n")
elif user_choice == 2:
hero_name = input("Please choose a name for yourself, adventurer: ")
hero = Character(hero_name)
print("I see, so your name is", hero.name + "... Very well. We shall begin your journey immediately!\n")
The code should work properly, and when I run the program in PyScripter, I get a nice output:
I see, so your name is (the name I chose)... Very well. We shall begin your journey immediately!
But, when I run the program from the cmd-prompt, I get:
... Very well. We shall begin your journey immediately!
Am I doing something wrong here? Or is this a fault on Windows?
I believe there's a typo in your print() statement:
# You have this:
print("I see, so your name is", hero.name + "... Very well. We shall begin your journey immediately!\n")
# Did you mean this?
print("I see, so your name is" + hero.name + "... Very well. We shall begin your journey immediately!\n")
The following works in Python 3.2 with Eric5 IDE:
heroname = "Joe Cool"
print("I see, so your name is", heroname + "... Very well. We shall begin your journey immediately!\n")

Ruby, how should I design a parser?

I'm writing a small parser for Google and I'm not sure what's the best way to design it. The main problem is the way it will remember the position it stopped at.
During parsing it's going to append new searches to the end of a file and go through the file startig with the first line. Now I want to do it so, that if for some reason the execution is interrupted, the script knows the last search it has accomplished successfully.
One way is to delete a line in a file after fetching it, but in this case I have to handle order that threads access file and deleting first line in a file afaik can't be done processor-effectively.
Another way is to write the number of used line to a text file and skip the lines whose numbers are in that file. Or maybe I should use some database instead? TIA
There's nothing wrong with using a state file. The only catch will be that you need to ensure you have fully committed your changes to the state file before your program enters a section where it may be interrupted. Typically this is done with an IO#flush call.
For example, here's a simple state-tracking class that works on a line-by-line basis:
class ProgressTracker
def initialize(filename)
#filename = filename
#file = open(#filename)
#state_filename = File.expand_path(".#{File.basename(#filename)}.position", File.dirname(#filename))
if (File.exist?(#state_filename))
#state_file = open(#state_filename, File::RDWR)
resume!
else
#state_file = open(#state_filename, File::RDWR | File::CREAT)
end
end
def each_line
#file.each_line do |line|
mark_position!
yield(line) if (block_given?)
end
end
protected
def mark_position!
#state_file.rewind
#state_file.puts(#file.pos)
#state_file.flush
end
def resume!
if (position = #state_file.readline)
#file.seek(position.to_i)
end
end
end
You use it with an IO-like block call:
test = ProgressTracker.new(__FILE__)
n = 0
test.each_line do |line|
n += 1
puts "%3d %s" % [ n, line ]
if (n == 10)
raise 'terminate'
end
end
In this case, the program reads itself and will stop after ten lines due to a simulated error. On the second run it should display the next ten lines, if there are that many, or simply exit if there's no additional data to retrieve.
One caveat is that you need to remove the .position file associated with the input data if you want the file to be reprocessed, or if the file has been reset. It's also not possible to edit the file and remove earlier lines or it will throw off the offset tracking. So long as you're simply appending data to the file, or restarting it, everything will be fine.

Resources