views:

137

answers:

2

I'm probably doing something stupid but I can't figure out what it is.

The output I'm seeing from this program is

foo

test

What I'm expecting to see is

foo
abc
test

Does anyone see anything obviously wrong here?

class Foo

    def initialize(l)
    @label = l
    end

    def label
    @label
    end

    def abc
    @abc
    end

    def abc=(abc)
    @abc
    end

end

foo = Foo.new("foo")
foo.abc=("abc")
puts foo.label
puts foo.abc
puts "test"
+7  A: 

You never set @abc in your abc= method. It should look like

def abc=(val)
  @abc = val
end
Logan Capaldo
Damn. I knew it was going to be something stupid. Thanks dude.
+4  A: 

In addition to Logan's answer which tells you what the error was, here's how that error never would have occurred in the first place:

class Foo
  def initialize(l)
    @label = l
  end

  attr_reader :label
  attr_accessor :abc
end
Jörg W Mittag