From: Rob Biedenharn Date: 2012-08-25T04:45:34+09:00 Subject: Re: learning by doing part 2 - tc game On Aug 24, 2012, at 3:26 PM, Sebastjan H. wrote: > Once again, I turn to you for your wisdom:) > > Why this works: > ----------------------------------------- > class Depo > attr_accessor :test > > def initialize(test) > @test = test > end > > def test() > @test > end > end > > bla = Depo.new("blahdsd") > > def defin (bla) > if defined? test > puts "#{bla.test}" > end > end > > defin(bla) > ------------------------------------------ > > And this doesn't: > ------------------------------------------ > def effect_output(card) > if defined? attack > puts "#{card.name.capitalize} hits with #{card.attack}." > elsif defined? heal > puts "#{card.name.capitalize} heals for #{card.heal}." > end > end > ------------------------------------------ > The card is a DRAGON class object with method attack defined. It has the > attr_accessor as well as initialize method defined. I don't even get an > error. Nothing gets putsed. > > If I use > > if defined? :attack > > then I get the message that attack is an undefined method for class > HEAL. > > ??? You don't want defined? here, you want respond_to? as in: if card.respond_to?(:attack) # do something involving card.attack end -Rob