Andrew
Andrew

Reputation: 16051

syntax error, unexpected '=', expecting keyword_end

I have this code:

class TVShow
    attr_accessor :name
    attr_accessor :tvdbID

    def initialize(showName)
        :name = showName
    end
end

And it gives me this error:

syntax error, unexpected '=', expecting keyword_end
        :name = showName

What I'm wanting to do is have a public variable i can use across the entire class, so that's what i'm trying to do with :name and :tvdbID.

I'm fairly new to ruby, so let me know if I'm doing this all wrong.

Upvotes: 0

Views: 6486

Answers (2)

bsd
bsd

Reputation: 2717

In Ruby attr_accessor is a shorthand for defining setters and getters(read mutators) of a field(instance variable). So if you wanted to create setter and getter for name. You would have to write extra boiler plate code around it. The class would look something like:

class TVShow
  def name=(name)
    @name = name
  end

  def name
    @name 
  end

#Similarly other instance variables.
#Instance variables always start with @

end

You would instantiate an object of the class with

tvshow = TVShow.new
tvshow.name = 'Stack Overflow'
puts "New tv show : #{tvshow.name}"

:attr_accessor :name creates instance methods for name. attr_accessor is a method in the Module class.

In Ruby there is no need to define instance variables at the start of the class(like you do in other languages like Java, C++ et al). So in your constructor you would have to change from a symbol to an instance variable

def initialize(name)
    @name = name
end

Upvotes: 0

7stud
7stud

Reputation: 48599

Change:

 def initialize(showName)
   :name = showName
 end

to

 def initialize(showName)
   @name = showName
 end

You can also do this:

attr_accessor :name, :tvdbID

Some examples:

class Dog
  def initialize(name)
    @name = name
  end

  def show
    puts "I am a Dog named: " + @name
  end

  def add_last_name(last_name)
    @name = @name + " " + last_name
  end

end

d = Dog.new "Fred"
d.show
d.add_last_name("Rover")
d.show

--output:--
I am a Dog named: Fred
I am a Dog named: Fred Rover

So instance variables are freely accessible from within the class. However, you cannot access the instance variables in the example above from outside the class:

d = Dog.new "Fred"
puts d.name

--output:--
1.rb:17:in `<main>': undefined method `name' for #<Dog:0x000001010a5b48 @name="Fred"> (NoMethodError)

Here is how you can access the instance variables from outside the class:

class Dog
  def initialize(name)
    @name = name
  end

  def name  #getter
    @name
  end

  def name=(val)  #setter
    @name = val
  end
end

d = Dog.new "Fred"
puts d.name

--output:--
Fred

Those getters and setters are a pain to type--especially if you have say 10 instance variables--so ruby provides a shortcut:

class Dog
  def initialize(name, age)
    @name = name
    @age = age
  end

  attr_accessor :name, :age

end

d = Dog.new("Fred", 5)
puts d.name
puts d.age
d.age = 6   #calls age=() method
puts d.age

--output:--
Fred
5
6

But it is customary to write the attr_accessor line at the beginning of a class.

Upvotes: 2

Related Questions