Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am currently developing a text-based RPG in Ruby, however there are some bugs in the programming code. The code is long, so I'll provide a link to the Source Code HERE.
The problem occurs at line 113. Whenever I enter "y" for case randEvent1 it accepts it and does the following lines in the when statement but when I put in "yes", it skips all of those lines goes to the line:
puts "Do you want to go to a Tavern next?"
puts "Or maybe you want to go to the forest?"
My question is, why does it skip the lines when I put in "yes" for randEvent1 even though I put that it will execute the following the when statement if randEvent1 == "y" || "yes"?
This problem also occurs when I put in "no" for the case RandEvent1.
Could it be possible it is a problem with doing || in a case..when statement? Is the syntax different for the or operator in case..when than in if statements?
Case statements do not accept || like ifs do. The proper syntax for a case having two or more possible inputs is ,. So, instead of doing case 'y' || 'yes', it should be case 'y', 'yes'.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to know if a string includes the form '+#someletter#+', where #someletter# is a
letter. If the string is 'dogs+d+', it would be true because it contains '+d+'. If the string is 'dogs++c', it would be false because the 'c' wasn't surrounded by '+' signs.
I was thinking it was something using regexp like
string.include? '+/a-z/+'
but that doesn't work. Please help.
include? does not accept a regex. To use a regex, you need a different method.
string =~ /\+[a-z]\+/
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I don't know why this isn't working properly, it always gives me false.
More importantly is there an easy way to see line-by-line execution of the code? Something like an easy trace that would help me troubleshoot this (I tried irb, but couldn't get it to work with multiline example like this one).
puts 'enter a word and I will tell you if its a palindrome or not: '
word = gets.chomp
backwards = word.reverse
if word == backwards
puts "yes, it is a palindrome!"
else
puts "no, #{word} is not a palindrome."
end
EDIT:
Sorry, I had a typo in my word. This is working fine. I feel like an idiot. Which brings me to my second question above... a good way to trace code execution in irb or elsewhere... is that possible?
The pry gem is an excellent debugging tool.
https://github.com/pry/pry
Install it by typing on the command line...
gem install pry
Then in your code at the point you want to halt execution, enter this line
require 'pry'; binding.pry
You can then inspect all variables, even change them if you want, and type 'exit' when you're ready to resume.
If you do require 'pry' at the beginning of your program, you can just do binding.pry where you need to halt... in that case you don't need the two-command expression I used above.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Ruby provides unless and elsif statements. It seems natural to assume that there would be a similar elsunless statement, but there is not. Is there a specific reason for this?
To illustrate, this statement would allow for code like this.
unless broken
# do something
elsunless done
# do something else
end
I'm aware that this code can be rewritten to use if and elsif, but in some cases using unless is clearer.
The logic behind if / else statements usually is:
Having one exception:
if exception_a
# do exception stuff
else
# do standard stuff
end
unless exception_a
# do standard stuff
else
# do exception stuff
end
Adding unless in this case can be very useful, as you can switch around your code. What I also love about unless is that you can solely do your standard stuff while checking for an exception. (the else block can be left out)
Having multiple exceptions:
Here comes the tricky part:
if exception_a
# do exception stuff a
elsif exception_b
# do exception stuff b
else
# do standard stuff
end
unless exception_a
# do standard stuff
elsunless exception_b
# do ???
else
# do exception stuff
end
Besides being totally unreadable, I couldn't find a logical meaning to the elsunless block: What code would you put in there? I still have no idea if that would be some exception stuff or standard code.
Maybe you can explain further what code you would use in such a block.
Ruby already provides if, else, elsif, and unless, so is there really a need to for elsunless? It looks like a hulking mammoth in a code. I think Matz doesn't see a reason to add the statement into a the ruby syntax.
Additionally, some of ruby coders investigate a ruby coding standard that excludes unless statement usage, which was inherited from Perl.
As for me, I would completely remove the unless keyword from the language.
Have a look at the styling guide
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my latest task, I found some code like this:
if false
print "a"
elsif true
print "b"
else
print "c"
end
Is if else statement correct? Will if ever be executed?
This code will always print "b".
One explanation is that this code was put in as a placeholder for some real logic which was supposed to be added at a later point in time.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Eclipse (RedRails) complain about "Feature envy" in the following code:
if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m
content_text = input_text[($1.size + $2.size)..-1] # warning in $1
header = YAML.load($1)
#content = content_text.strip()
#title = header["title"]
end
My understanding is that I safe to ignore this warning. But I am wandering why this warning is generated. I cannot understand how I can extract method for $1.size and $1.
Reek is telling you that, because you are adding two properties of the same class, the calculation should actually belong in String. When adding string lengths this is nonsense of course, but in your case the code can be simplified by using $& (the complete matched string):
input_text[$&.size..-1]