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.

第八・九回:イコール判別の関数作成

Last updated at Posted at 2020-12-31

目標

assert_equalを作成する.

はじめに…

以下のようにassert_equal.rbを作成する.

p 1==1

実行すると以下のようになる.

$ ruby assert_equal.rb
true

1=1は正しいので,trueと出る.

assert_equalを作る.

では,以下のように変更する.

def assert_equal expected, result
   return expected==result
end

p assert_equal 1,1

この場合,括弧は省略されている.

実行すると以下のようになる.

$ ruby assert_equal.rb
true

次のように変更する.

def assert_equal expected, result
   if expected==result
      puts 'true'
   else
      puts 'fasle'
   end
end

assert_equal 1,1
assert_equal 1,2

これで実行すると,以下のようになる.

$ ruby assert_equal.rb
true
false

色付け

上記の結果表示でもよいのだが,色を付ける.先ほど作成したassert_equal.rbを変更する.

require 'colorize'
def assert_equal expected, result
   if expected==result
      puts 'true'.green
   else
      puts 'fasle'.red
   end
end

assert_equal 1,1
assert_equal 1,2

こうすることで,trueの時は緑,falseの時は赤でそれぞれtrue,falseが表示されるようになる.

参考サイト

参考にしたサイトは以下の通り.


  • source ~/grad_members_20f/members/yuhsuzu/r8and9.org
1
0
0

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?