From: "Sebastjan H." Date: 2012-08-26T22:12:24+09:00 Subject: Re: learning by doing part 2 - tc game Jan E. wrote in post #1073054: > Hi, > > The "defined?" operator is simply wrong in this case. It checks *the > current scope* for variables, methods etc. > > For example: > > x = 0 > C = 15 > puts defined? x # this outputs "local-variable" > puts defined? C # this outputs "constant" > puts defined? a # this outputs nothing (because the result is nil) > > You want something completely different, namely check if an object has a > specific method. That's what Object#respond_to? is for: > > def effect_output(card) > if card.respond_to? :attack > puts "#{card.name.capitalize} hits with #{card.attack}." > elsif card.respond_to? :heal > puts "#{card.name.capitalize} heals for #{card.heal}." > end > end > > This will call "attack" and "heal" only if the card object does actually > have those methods. > > What should have made you suspicious is that you never had to specify > any object. How is Ruby then supposed to know that you want to look into > the card object? > > By the way, the reason why your program output something after you used > a symbol is that > > defined? :attack > > returns "expression" (which is truthy). Thank you. That helped. I actually was wondering why I didn't have to specify an object, but I just couldn't make the connection. Anyway, I think I am finished with the game for now. It runs ok and I've also included a couple of different arenas which influence on the possibility of critical strikes. It's fun to play regarding it's my first game and it runs in the command line:) I also rewrote all the parts where I misused the instance variables. kind regards, seba -- Posted via http://www.ruby-forum.com/.