Basically, while writing ruby codes, I avoid using inheritance because inheritance sometimes produce unnecessary tight couplings.

Example

class Vehicle   
def start_engine
end
def stop_engine
end
end

class Car < Vehicle
def drive
start_engine
stop_engine
end
end

In this case, Car class knows about the codes of Vehicle class. If we create objects that don’t use an engine, it will necessary to take much time to change the codes.

Solution

We should use compositions. In other words, we should define Engine object.

class Car   
def initialize
@engine = Engine.new
end
def drive
@engine.start
@engine.stop
end
end
class Engine
def start
end
def stop
end
end

In this case, Engine class was separated from Car class. Moreover, it has become easy to make new objects that don’t use an engine.

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--

Basically, “bundle exec” command precedes rails commands. My subordinate asked me about the necessity of this command. I’m going to write it down here.

Rails has gemfile.lock file that solves dependencies among gems. When we use bundle exec command, gemfile.lock file is referred to. Instead of that, if we don’t…

--

--

We sometimes have to read library codes for curiosity or bug fixes. However, there are some techniques needed, so I’ll write them down here.

Set up path for installing gems

bundle config set path ‘vendor/bundle’ —- local

Codes of library become visible in our projects and it has become possible to insert pry codes into libraries.

Find where methods are defined

class Book
def read
end
end
> Book.new.method(:read).source_location
=> ['book.rb', 2]

Use debuggers

・binding.irb

・binding.pry

・byebug

Find a caller

・puts caller

class BooksController < ApplicationController
def index
puts caller
end
end
# caller is default in Ruby

・backtrace(this is in byebug)

That’s all! Make debugging more effective by using techniques above.

My LinkedIn account is below! Please contact me!

https://www.linkedin.com/in/tomoharu-tsutsumi-56051a126/

--

--