Multiscale simulation 11

!Mac OS X-10.15.7!ruby-2.7.2p137

hello class

Lecture page link

Chart type ruby-VI (hello class)

Contents

Create class

Task

Classify assert \ _equal and Monkey Patch to Integer

Answer example

#!/usr/bin/env ruby
# frozen_string_literal: true

class Integer
  def assert_equal(target)
    if self == target
      puts true
    else
      puts false
    end
  end
end

puts 3.assert_equal 3

output

> ruby assert_equal.rb
true

NOTE

--It seems that class overwriting is called monkey patching. --Use class to make the code easier to read --Become object-oriented


Recommended Posts

Multiscale simulation 11
Special Lecture on Multi-Scale Simulation: 11th (class)
Special Lecture on Multiscale Simulation: 8th (assert)
Special Lecture on Multiscale Simulation: 10th (Recursive)