Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Ruby, help with classes and pointing to methods?
So, I'm fairly new to ruby. I have done all online tutorials and I understand all the basic concepts and aspects of the language. My problem is, I'm trying to write this code. I've had this problem trying different codes as well, here it is
class Battles
def initialize
@hp = 10
end
def start
puts 'Type 1 to attack:'
input = gets
if input == 1
dmg
end
end
def showing
puts @hp
end
def dmg
@hp = @hp - rand(5)
puts @hp
end
def attack
if input == 1
@hp = @hp - @dmg
end
end
end
battle = Battles.new
battle.start
Everything starts up and such, but when I type 1 to attack, it doesn't go to the method dmg it just ends. I have also tried to do 1 as a 'string'. Any help?
2 Answers
- Anonymous1 decade agoFavorite Answer
Hi, whenever you don't seem to be getting what you think you should be getting in Ruby, something that you can do is use the p function. It is like calling puts on something, except that instead of calling the to_s method, it calls the inspect method.
By changing the start function to this
def start
puts 'Type 1 to attack:'
input = gets
p input
dmg if input == 1
end
Notice that I added the line "p input" in there. Then when we run the program, we see
Type 1 to attack:
1
"1\n"
From this, we can see that our input is not the integer 1, but rather it is the string "1\n"
-----
So, what do we do? There are a number of options, if you are really wanting your input to be an integer, we can just call the .to_i method on it like this:
def start
puts 'Type 1 to attack:'
input = gets.to_i
dmg if input == 1
end
This says to convert the string into an integer, it will convert any digits at the beginning of the string, and stop when it comes to any non digit, such as our newline character. You can see it's documentation here http://ruby-doc.org/core/classes/String.html#M0007...
Notice the example in the documentation:
"99 red balloons".to_i #=> 99
-----
Another thing we could do is this:
def start
puts 'Type 1 to attack:'
input = gets.chomp
dmg if input == '1'
end
This calls the .chomp method on the input, which leaves the input as a string, but removes any newlines at the end of it. You can see it's documentation here http://ruby-doc.org/core/classes/String.html#M0008...
Notice the example in the documentation:
"hello\n".chomp #=> "hello"
So, hope that helps, and good luck :)
- Anonymous5 years ago
Let's see... 1. Fire all women teachers considering the Bible says that women shall not teach over a man. 2. Make the Bible the ONLY textbook allowed. 3. Stone sinners. This means anyone who lies, steals, commits adultery(which, biblically speaking, means so much as being with the opposite sex unchaperoned), worships graven images(this means no more drooling over sexy Hollywood stars and the like), covets(so if you want something someone else has, you're done), etc. 4. Tear down our schools and replace them with churches so instead of going to school everyday to learn, you go to church.(Eh, I suppose this sort of goes with #3) 5. Serving biblically approved lunches(no more pork chops and ham sandwiches as pork is forbidden in the Bible) 6. Post the 10 Commandments in the hallways(although I've heard some schools actually try to get away with this) 7. Where there were once pictures of US Presidents, put only a picture of Christ. 8. During Lent, make fasting a requirement so goodbye snacktime(for younger kids) and lunches. 9. Instead of school counselors, make the kids go to confession. 10. For celebrating holidays, make them ONLY celebrate religious ones and ONLY the religious aspects. Halloween? Buh-bye. Thanksgiving? A day of prayer. Christmas? Sayonara Santa and presents. Have a Nativity pageant. Easter? No more eggs and bunnies, you get a recreation of the Passion. 11. Require daily prayers or recitation of things such as the Lord's Prayer. 12. Instead of teachers, give them priests/ministers/etc. 13. Field trips are to monasteries, places of religious significance, instead of to someplace fun. 14. Separate boys and girls classes, make the girls wear long hair and wear dresses or "modest" clothing. No makeup, no jewelry...unless it's a crucifix or something religious, of course. 15. Homework involves essays as to why other religions are going to hell and the only things worse than another faith is no faith at all, or on how a person can end up in hell or how they can avoid it, etc. 16. Before meals, everyone must say grace/table prayers, and those who don't pray, don't eat. Honestly, I could probably keep going all day if I tried...