I know this is sloppy code, but here it is:
display dialog ("Start Screensaver. Please type: matrix, coffee, waffles, star, water, or
fireworks.", default answer "")
if text returned of result = "matrix" then
set user_choice to "MatrixSaver"
else
if text returned of result = "coffee" then
set user_choice to "Coffee"
else
if text returned of result = "waffles" then
set user_choice to "Waffles"
else
if text returned of result = "star" then
set user_choice to "Hyperspace"
else
if text returned of result = "water" then
set user_choice to "LotsaWater"
else
if text returned of result = "fireworks" then
set user_choice to "Skyrocket"
else
(*do nothing*)
end if
end if
end if
end if
end if
end if
if (user_choice = null) then (*do nothing*)
else
tell application "System Events"
set ss to screen saver user_choice
start ss
end tell
end if
When I'm trying to compile my code, the 'default answer' Is highlighted, and it says: "Expected “)”, etc. but found identifier."
Any Ideas? Thanks.
I believe the correct syntax is just
display dialog "Start Screensaver. Please ..." default answer ""
The , between the ("Start Screensaver") and the default answer parameter is causing the syntax error. Remove the ,.
This isn't a syntax error, but the variable user_choice doesn't exist outside of the big if block. If you ran it as written, you would get this message at the last if block:
The variable user_choice is not defined.
You could fix this by declaring the variable before the display dialog statement...
set the user_choice to ""
Now you can use the variable anywhere in the code. :)
Related
I want to have my program print an alternate string if the user's input is incorrect five times. The code I'm using below is giving me undefined method `+' for nil:NilClass (NoMethodError) referring to += and I'm not sure why.
loop do
input = gets.chomp
if input =~ /\d/
#long case statement here
else
annoyed += 0
if annoyed == 5
puts "alternate prompt"
else
puts "normal prompt"
end
end
end
#radubogdan, in a comment, has explained the problem with your code. Consider writing it something like the following.
wrong_answers = 0
loop do
print wrong_answers < 5 ?
"Will you agree to tell me who your handler is?: " :
"Your life is toast if you don't tell me. Will you tell me now?: "
if gets.chomp.match?(/yes/i)
puts "You've come to your senses"
puts "executing code..."
break
end
puts "You're lying"
wrong_answers += 1
end
The following conversation might take place.
Will you agree to tell me who your handler is?: no
You're lying
Will you agree to tell me who your handler is?: No!
You're lying
Will you agree to tell me who your handler is?: nyet
You're lying
Will you agree to tell me who your handler is?: shove it
You're lying
Will you agree to tell me who your handler is?: never!
You're lying
Your life is toast if you don't tell me. Will you tell me now?: don't hit me again
You're lying
Your life is toast if you don't tell me. Will you tell me now?: #%$ **#
You're lying
Your life is toast if you don't tell me. Will you tell me now?: yes
You've come to your senses
executing code...
I wrote print arg where:
arg = wrong_answers < 5 ? "Will you agree..." : "Your life is toast..."
The right side of this expression employs a ternary operator.
I have a program that displays a numbered list and asks the user to input either a number or name from the list, and loops a block until the user enters "exit", after which it ends.
I want to add a line or two that puts an error message like, "Sorry, I don't seem to understand your request" if the user inputs something that is not on the list (name/number) and is not the word "exit".
I can't seem to figure it out. Any advice? My current code is below.
def start
display_books
input = nil
while input != "exit"
puts ""
puts "What book would you more information on, by name or number?"
puts ""
puts "Enter list to see the books again."
puts "Enter exit to end the program."
puts ""
input = gets.strip
if input == "list"
display_books
elsif input.to_i == 0
if book = Book.find_by_name(input)
book_info(book)
end
elsif input.to_i > 0
if book = Book.find(input.to_i)
book_info(book)
end
end
end
puts "Goodbye!!!"
end
Seems that you should add an elsif statement in this if:
if book = Book.find_by_name(input)
book_info(book)
elsif input != 'exit'
puts "Sorry, I don't seem to understand your request"
end
A good template for an interpreter is to build around Ruby's very capable case statement:
loop do
case (gets.chomp.downcase)
when 'list'
display_books
when /\Afind\s+(\d+)/
if book = Book.find($1.to_i)
book_info(book)
end
when /\Afind\s+(.*)/
if book = Book.find_by_name($1)
book_info(book)
end
when 'exit'
break
else
puts "Not sure what you're saying."
end
end
Although this involves regular expressions, which can be a bit scary, it does give you a lot of flexibility. \A represents "beginning of string" as an anchor, and \s+ means "one or more spaces". This means you can type in find 99 and it will still work.
You can create a whole command-line interface with it if you take the time to specify the commands clearly. Things like show book 17 and delete book 17 are all possible with a bit of tinkering.
How can I check if a variable is a number?
I'm trying this:
set a to 5
if a is a number
display dialog "Yes! It's a number!"
end if
I've also tried this code:
set a to 5
if a is integer
display dialog "Yes! It's a number!"
end if
But unfortunately it doesn't work as expected.
set a to 5
if class of a is integer then
display dialog "Yes! It's a number!"
end if
class of a is integer will fail if you use
set a to "5"
This will work if even if the variable is a number but was entered as text.
set a to "5"
try
set a to a as number
display dialog "Yes! It's a number!"
end try
This is my solution:
on is_number(number_string)
try
set number_string to number_string as number
return true
on error
return false
end try
end is_number
My current code is this:
print "Feed me input."
def get_input
input_value=gets.chomp
if !input_value
print "you didn't type anything"
else
input_value.downcase!
if input_value.include? "s"
input_value.gsub!(/s/,"th")
else
print "You entered a string but it had no 's' letters."
end
end
return input_value
end
get_input()
if !get_input
get_input
else
puts "#{get_input}"
end
I'm not sure why it isn't working. When I run it I get prompted for input then when I press enter after entering none I get the "You entered a string but it had no 's' letters", not the "you didn't type anything" that I wanted.
Every object except false and nil is treated as false if they are used as predicates. Even empty string is treated as true:
s = ""
puts true if s # => true
Use String#empty? to check if it is empty string.
As you said When I run it I get prompted for input then when I press enter after entering none - It means what happened acctually is
input_value="\n".chomp #( you gets methods take only `\n` as input)
"\n".chomp # => ""
so your input_value variable holds and empty string object. Now in Ruby every object has true value, except nil and false. Said that "" is also true,but you did !input_value,which means you are making it false explicitly. That's the reason in the below if-else block, else part has been executed and you didn't see the expected output "you didn't type anything".
if !input_value
print "you didn't type anything"
else
input_value.downcase!
if input_value.include? "s"
#.. rest code.
So I would suggest you in such a context replace the line if !input_value to if input_value.empty?, Which will make your code to behave as you are expecting. I didn't take your logic as a whole,but tries to show you how to code to meet your needs:
print "Feed me input."
def get_input
input_value=gets.chomp
if input_value.empty?
puts "you didn't type anything"
false
else
puts "found value"
input_value.downcase!
end
end
until input = get_input
# code
end
puts input
output
kirti#kirti-Aspire-5733Z:~/Ruby$ ruby test.rb
Feed me input.
you didn't type anything
you didn't type anything
you didn't type anything
HH
found value
hh
kirti#kirti-Aspire-5733Z:~/Ruby$
If i give input as 1 or 2, regardless of that program goes in default. Tried comparing input with "1" and 1 both. Same result.
My first Ruby program, plz excuse for naivety.
$choice
def getInfo
puts "Info"
end
def getMoreInfo
puts "MoreInfo"
end
def switch
if $choice == "1" #intentionally in ""
getInfo
elsif $choice == 2 #intentionally without ""
getMoreInfo
else
puts "default"
end
end
def callMainMenu
puts "Choose the operation:"
puts "[1] Get some Info"
puts "[2] Get some moreInfo"
$choice=gets
$choice.chomp
end
callMainMenu
switch
You need to use the destructive version of chomp if you're going to assign it like that.
$choice.chomp!
Or
$choice = $choice.chomp
In order to debug this, what I'd do is add puts $choice.inspect at the beginning of your switch method to see exactly what's in the variable. That said, I believe the problem here is that you're calling $choice.chomp instead of $choice.chomp!. The former will return the result, and the latter will change the variable in place.
When you change $choice.chomp to $choice.chomp! and get rid of the // (change those to #), then you'll have something working. Keep refining it , it is not perfect yet.
Use $choice.chomp!. chomp without ! does not alter $choice. It returns a new string. This a naming convention in Ruby.