Error while creating an object

0 Ahmed Tayar · October 23, 2015
Whenever I create a Class I have the below issue:

irb(main):002:0> class Animal
irb(main):003:1> attr_acessor = :name, :type, :age
irb(main):004:1> end
=> [:name, :type, :age]
irb(main):005:0> class Dog < Animal
irb(main):006:1> end
=> nil
irb(main):007:0> class Fish < Animal
irb(main):008:1> end
=> nil
irb(main):009:0> class Cat < Animal
irb(main):010:1> end
=> nil
irb(main):011:0> fish = Fish.new
=> #<Fish:0x00000002bd6e40>
irb(main):012:0> fish.name = "name"
NoMethodError: undefined method `name=' for #<Fish:0x00000002bd6e40>
        from (irb):12
        from C:/Ruby22-x64/bin/irb:11:in `<main>'
irb(main):007:0> class Fish < Animal
irb(main):008:1> end
=> nil
irb(main):009:0> class Cat < Animal
irb(main):010:1> end
=> nil
irb(main):011:0> fish = Fish.new
=> #<Fish:0x00000002bd6e40>
irb(main):012:0> fish.name = "name"
NoMethodError: undefined method `name=' for #<Fish:0x00000002bd6e40>
        from (irb):12
        from C:/Ruby22-x64/bin/irb:11:in `<main>'
irb(main):013:0> animal = Animal.new
=> #<Animal:0x000000029c1da8>
irb(main):014:0> animal.name = "Loly"
NoMethodError: undefined method `name=' for #<Animal:0x000000029c1da8>
        from (irb):14
        from C:/Ruby22-x64/bin/irb:11:in `<main>'
irb(main):015:0>

Post a Reply

Replies

Oldest  Newest  Rating
0 Cristiano Cappagli · October 23, 2015
Hi :)
There are 2 errors in your code:
first you typed wrong attr_accessor with only one c instead of two.
second instead of:

attr_accessor = :name, :type, :age

it's:
attr_accessor :name, :type, :age

Try it now and it will work fine :D
  • 1

Ruby

107,107 followers
About

Write web apps quickly and easily with this easy to learn language.

Links
Moderators
Bucky Roberts Administrator