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- @@total_cars_created =
0
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.
-
.total_cars_created ⇒ Integer
Returns how many SimpleCar instances have been constructed.
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.
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/simple_car.rb', line 74 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 = [] @@total_cars_created += 1 end |
Instance Attribute Details
#id ⇒ String (readonly)
Returns The string identifier for the car.
63 64 65 |
# File 'lib/simple_car.rb', line 63 def id @id end |
#make ⇒ String (readonly)
Returns The make of the car.
63 |
# File 'lib/simple_car.rb', line 63 attr_reader :id, :make, :model, :year |
#model ⇒ String (readonly)
Returns The model of the car.
63 |
# File 'lib/simple_car.rb', line 63 attr_reader :id, :make, :model, :year |
#year ⇒ Object (readonly)
Returns the value of attribute year.
63 |
# File 'lib/simple_car.rb', line 63 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.
41 42 43 |
# File 'lib/simple_car.rb', line 41 def create_default new("DefaultMake", "DefaultModel", 2024) end |
.get_default_makes ⇒ Array<String>
Returns the list of default makes used in sample data.
48 49 50 |
# File 'lib/simple_car.rb', line 48 def get_default_makes %w[Toyota Tesla Ford Honda Chevrolet] end |
.total_cars_created ⇒ Integer
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 Method Details
#add_mileage_entry(miles) ⇒ void
This method returns an undefined value.
Appends a mileage reading to the in-memory history.
129 130 131 |
# File 'lib/simple_car.rb', line 129 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.
164 165 166 167 168 169 170 |
# File 'lib/simple_car.rb', line 164 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.
136 137 138 |
# File 'lib/simple_car.rb', line 136 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.
152 153 154 |
# File 'lib/simple_car.rb', line 152 def @tags.dup end |
#is_running ⇒ Boolean
Gets a value indicating whether the car is currently running (started).
97 98 99 |
# File 'lib/simple_car.rb', line 97 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.
144 145 146 |
# File 'lib/simple_car.rb', line 144 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.
109 110 111 |
# File 'lib/simple_car.rb', line 109 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.
118 119 120 |
# File 'lib/simple_car.rb', line 118 def stop @is_running = false end |
#to_s ⇒ String
Returns a string that represents the current car.
191 192 193 |
# File 'lib/simple_car.rb', line 191 def to_s "#{@id} #{@year} #{@make} #{@model} (SimpleCar) #{@is_running ? "Running" : "Not Running"}" end |
#wheels ⇒ Integer
Gets the number of wheels on the car.
90 91 92 |
# File 'lib/simple_car.rb', line 90 def wheels DEFAULT_WHEELS end |
#with_year(new_year) ⇒ SimpleCar
Return a copy with an updated model year.
178 179 180 181 182 183 184 |
# File 'lib/simple_car.rb', line 178 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 |