Elements of Class - names? can they vary?

Hiya All,

Possibly on odd question but indulge a newbie :slight_smile: When writing Classes
each of the elements must have a name? Would anyone know what they are?
(It just really helps if I can talk-through-the-c0de and not say
‘thingy’)

For example below, we have wodks like make, model and color repeated 4
times. What are they called? @make is an instance variable, but the
others?

Also, can you vary them? Say: @make_of_car = make ? I tried but it
seems they must be called the same. I’m not sure why :slight_smile:


class Car

This class contains has the bits named differently

attr_accessor :make, :model, :color

def initialize(make, model, color)
@make = make
@model = model
@color = color
end

end

escort = Car.new(“Ford”,“Escort”,“Red”)

puts “It’s a #{escort.color} #{escort.make} #{escort.model}”


DE

Dave E. wrote in post #1048463:

Hiya All,

Possibly on odd question but indulge a newbie :slight_smile: When writing Classes
each of the elements must have a name? Would anyone know what they are?
(It just really helps if I can talk-through-the-c0de and not say
‘thingy’)

For example below, we have wodks like make, model and color repeated 4
times. What are they called? @make is an instance variable, but the
others?

Also, can you vary them? Say: @make_of_car = make ? I tried but it
seems they must be called the same. I’m not sure why :slight_smile:


class Car

This class contains has the bits named differently

attr_accessor :make, :model, :color

def initialize(make, model, color)
@make = make
@model = model
@color = color
end

end

escort = Car.new(“Ford”,“Escort”,“Red”)

puts “It’s a #{escort.color} #{escort.make} #{escort.model}”


DE
Strictly speaking what you call “elements” of a class are “attributes”
of that class. They can have any name you like - the problem with your
example is they are defined automatically by the attr-accessor
statement, so this is where you have to change them.
i.e. attr_accessor :car_make, :car_model, :car_color
This will change the attributes to your new names.