Dependency injection in ruby with example

class Car
attr_reader :mileage

def initialize(mileage)
mileage = mileage
end
def worn_out
mileage * Tire.new(“Maxis”, 60, 34).radius
end
end
class Tire
attr_reader :brand, :radius, :width
def initialize(brand, radius, width)
brand = brand
width = width
radius = radius
end
end
class Car
attr_reader :mileage, :tire
def initialize(mileage, tire)
mileage = mileage
tire = tire
end
def worn_out
mileage * tire.radius
end
end
class Tire
attr_reader :brand, :radius, :width
def initialize(brand, radius, width)
brand = brand
width = width
radius = radius
end
end
Car.new(20000, Tire.new(“Maxis”, 60, 34))

--

--

--

Software Developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Bounce Finance Guide- Presale

Introduction to N-SCOSS Rating

Comprehensive Guide on Integrating Open Policy Agent (OPA) With Kubernetes

Introduction to Entity storage for Games using ECS (or DCS)

Use Case Analysis — Real-time Precision Marketing and Group Selection

Job Sequencing with Deadline in O(nlogn) complexity

How to setup VIM for XCode

How to Build a Metaverse Decentralized Applications (dApp) with Unity?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aydar Omurbekov

Aydar Omurbekov

Software Developer

More from Medium

Testing a Rails API with RSpec

Setup Ruby on R

Building a Ruby CLI app

CURD actions with Ruby Sinatra