Class: SimpleCar
Overview
Represents a simplified car that uses a string identifier instead of a GUID. Includes the Vehicle module and covers:
primitive fields, static members, constructors, primitive-array I/O,
and methods that accept / return complex types from the same library.
Constant Summary collapse
- DEFAULT_WHEELS =
Default number of wheels for a SimpleCar.
4
Class Attribute Summary collapse
-
.total_cars_created ⇒ Integer
readonly
Returns how many SimpleCar instances have been constructed.
Instance Attribute Summary collapse
-
#id ⇒ String
readonly
The string identifier for the car.
-
#make ⇒ String
readonly
The make of the car.
-
#model ⇒ String
readonly
The model of the car.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Class Method Summary collapse
-
.create_default ⇒ SimpleCar
Creates a car with placeholder make, model, and the current year.
-
.get_default_makes ⇒ Array<String>
Returns the list of default makes used in sample data.
Instance Method Summary collapse
-
#add_mileage_entry(miles) ⇒ void
Appends a mileage reading to the in-memory history.
-
#duplicate ⇒ SimpleCar
Returns a deep copy of this car, including runtime collections.
-
#get_mileage_history ⇒ Array<Integer>
Returns the recorded mileage history for the car.
-
#get_tags ⇒ Array<String>
deprecated
Deprecated.
Prefer reading tags from domain-specific repositories in new code.
-
#initialize(make, model, year, id = nil) ⇒ SimpleCar
constructor
Initializes a new car with identity fields and default runtime state.
-
#is_running ⇒ Boolean
Gets a value indicating whether the car is currently running (started).
-
#set_tags(tags) ⇒ void
Replaces the car’s tags with the given list.
-
#start ⇒ void
Starts the engine and marks the car as running.
-
#stop ⇒ void
Stops the engine and marks the car as not running.
-
#to_s ⇒ String
Returns a string that represents the current car.
-
#wheels ⇒ Integer
Gets the number of wheels on the car.
-
#with_year(new_year) ⇒ SimpleCar
Return a copy with an updated model year.
Constructor Details
#initialize(make, model, year, id = nil) ⇒ SimpleCar
Initializes a new car with identity fields and default runtime state.
When id is omitted or blank, a UUID string is generated automatically.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/simple_car.rb', line 72 def initialize(make, model, year, id = nil) @id = id.nil? || id.strip.empty? ? SecureRandom.uuid : id @make = make @model = model @year = year @is_running = false @mileage_history = [] @tags = [] self.class.instance_variable_set(:@total_cars_created, self.class.total_cars_created + 1) end |
Class Attribute Details
.total_cars_created ⇒ Integer (readonly)
Returns how many SimpleCar instances have been constructed.
The counter increments on every successful call to #initialize and is shared process-wide for the loaded module.
29 30 31 |
# File 'lib/simple_car.rb', line 29 def total_cars_created @total_cars_created end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns The string identifier for the car.
61 62 63 |
# File 'lib/simple_car.rb', line 61 def id @id end |
#make ⇒ String (readonly)
Returns The make of the car.
61 |
# File 'lib/simple_car.rb', line 61 attr_reader :id, :make, :model, :year |
#model ⇒ String (readonly)
Returns The model of the car.
61 |
# File 'lib/simple_car.rb', line 61 attr_reader :id, :make, :model, :year |
#year ⇒ Object (readonly)
Returns the value of attribute year.
61 |
# File 'lib/simple_car.rb', line 61 attr_reader :id, :make, :model, :year |
Class Method Details
.create_default ⇒ SimpleCar
Creates a car with placeholder make, model, and the current year.
Useful for demos and graft smoke tests where explicit vehicle details are not important.
39 40 41 |
# File 'lib/simple_car.rb', line 39 def create_default new("DefaultMake", "DefaultModel", 2024) end |
.get_default_makes ⇒ Array<String>
Returns the list of default makes used in sample data.
46 47 48 |
# File 'lib/simple_car.rb', line 46 def get_default_makes %w[Toyota Tesla Ford Honda Chevrolet] end |
Instance Method Details
#add_mileage_entry(miles) ⇒ void
This method returns an undefined value.
Appends a mileage reading to the in-memory history.
127 128 129 |
# File 'lib/simple_car.rb', line 127 def add_mileage_entry(miles) @mileage_history << miles end |
#duplicate ⇒ SimpleCar
Returns a deep copy of this car, including runtime collections.
The duplicate shares the same id and field values as the source instance.
162 163 164 165 166 167 168 |
# File 'lib/simple_car.rb', line 162 def duplicate copy = SimpleCar.new(@make, @model, @year, @id) copy.instance_variable_set(:@is_running, @is_running) copy.instance_variable_set(:@mileage_history, @mileage_history.dup) copy.instance_variable_set(:@tags, @tags.dup) copy end |
#get_mileage_history ⇒ Array<Integer>
Returns the recorded mileage history for the car.
134 135 136 |
# File 'lib/simple_car.rb', line 134 def get_mileage_history @mileage_history.dup end |
#get_tags ⇒ Array<String>
Prefer reading tags from domain-specific repositories in new code.
Returns a defensive copy of the current tags.
150 151 152 |
# File 'lib/simple_car.rb', line 150 def @tags.dup end |
#is_running ⇒ Boolean
Gets a value indicating whether the car is currently running (started).
95 96 97 |
# File 'lib/simple_car.rb', line 95 def is_running @is_running end |
#set_tags(tags) ⇒ void
This method returns an undefined value.
Replaces the car’s tags with the given list.
142 143 144 |
# File 'lib/simple_car.rb', line 142 def () @tags = ? .dup : [] end |
#start ⇒ void
This method returns an undefined value.
Starts the engine and marks the car as running.
Calling this method twice in a row leaves the car running; it does not raise when already started.
107 108 109 |
# File 'lib/simple_car.rb', line 107 def start @is_running = true end |
#stop ⇒ void
This method returns an undefined value.
Stops the engine and marks the car as not running.
Idempotent: stopping an already stopped car is allowed.
116 117 118 |
# File 'lib/simple_car.rb', line 116 def stop @is_running = false end |
#to_s ⇒ String
Returns a string that represents the current car.
189 190 191 |
# File 'lib/simple_car.rb', line 189 def to_s "#{@id} #{@year} #{@make} #{@model} (SimpleCar) #{@is_running ? "Running" : "Not Running"}" end |
#wheels ⇒ Integer
Gets the number of wheels on the car.
88 89 90 |
# File 'lib/simple_car.rb', line 88 def wheels DEFAULT_WHEELS end |
#with_year(new_year) ⇒ SimpleCar
Return a copy with an updated model year.
176 177 178 179 180 181 182 |
# File 'lib/simple_car.rb', line 176 def with_year(new_year) copy = SimpleCar.new(@make, @model, new_year, @id) copy.instance_variable_set(:@is_running, @is_running) copy.instance_variable_set(:@mileage_history, @mileage_history.dup) copy.instance_variable_set(:@tags, @tags.dup) copy end |