reverse method is always false? [closed] - ruby

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.

Related

Why is hello_world not working in Ruby on Ubuntu? [closed]

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 1 year ago.
Improve this question
My very first Ruby program: hello_world.rb:
puts ¨Hello World¨
I have saved it and when I try to run it from terminal, it returns:
ruby hello_world.rb
hello_world.rb:1:in `<main>': uninitialized constant World¨ (NameError)
I tried installing reinstalling ruby AND the editor(ATOM). That is really all I could think of to try. Any ideas would really help
-ruby 3.0.1p64. Ubuntu
Your string is not wrapped in double-quotes (") but in ¨ characters.
Replace
puts ¨Hello World¨
with
puts "Hello World"
You probably copy as pasted puts "Hello World" from some website whose formatting changes the quotes. They are not valid quotes for strings. Retype the quotes yourself.

load 'rubyfile.rb" in irb vs. pry [closed]

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 7 years ago.
Improve this question
I created a completely empty ruby file emptyrubyfile.rb, saved it, and then opened my terminal app, dropped into $ irb, and loaded it > load 'emptyrubyfile.rb'. The output returned was => > true, which seems, to me, to be the expected output.
I then exited irb, > exit, and dropped into pry $ pry. I loaded the same empty file > load 'emptyrubyfile.rb', but the output returned was => *. To escape the * I can use either exit or quit.
I realize that irb and pry, are similar, but different. My question is: what is the difference and why does the pry output expect more input from me and what is it asking me for at the * prompt?
You would have missed the ending ' while loading it in pry. There is no difference between irb and pry on this aspect. I'm guessing you typed
> load 'emptyrubyfile.rb

Why is there no `elsunless` statement in Ruby? [closed]

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

Bug in Ruby RPG (skips line in case..when) [closed]

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'.

Why does Eclipse complain about "Feature envy" smell in my code? [closed]

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]

Resources