1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

マルチスケールシミュレーション11

Last updated at Posted at 2020-12-26

!Mac OS X-10.15.7 !ruby-2.7.2p137

hello class

講義ページリンク

チャート式ruby-VI(helloclass)

内容

classを作成する

課題

assert_equalをクラス化してIntegerにMonkey Patchingする

解答例

#!/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

出力

> ruby assert_equal.rb
true

NOTE

  • クラスの上書きをmonkey patchingというらしい
  • コードを見やすくするにはclassとか使おう
  • object指向になれておこう

  • source ~/multiscalesim_toku/grad_members_20f/members/lynd2299/mss11.org
1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?