[RUBY] 9th

!Ubuntu-20.04.1!ruby-2.7.1p83

This is a summary site for lectures. If you see it, please push LGTM.

What i did

assert_equal

First, assert \ _equal is a function that confirms "equal or not".

theme

Judge 1 == 1

The simplest solution

p 1==1

In the case of ruby, true is returned with only this one line. It's easy.
Make this a method

def assert_equal(expected, result)
 return expected == result
end
p 1
p assert_equal(1,1)
p 2
p assert_equal(1,2)

Then

> 1
> true
> 2
> false

Is properly judged

Color the letters

It's off the main line, but it seems that you can try coloring the output

require 'colorize'
def assert_equal(expected,result)
  if expected == result
    puts 'true'.magenta
  else
    puts 'false'.cyan
  end                                                                                          
end
p 1
p assert_equal(1,1)
p 2
p assert_equal(1,2)

When you run

> 1
> true
> nil
> 2
> false
> nil

I don't know the color with qiita ... It's colored but nil is output. That means that something has not been assigned somewhere. When I thought something was wrong, nil disappeared except for the leading p of assert \ _equal. Did something happen around colorize?

rich output

-Output the values ​​of expected and result fetched as arguments
-Display a message when true or false. Realize

require 'colorize'
def assert_equal(expected,result)
  p ['expected',expected]
  p ['result',result]
  if expected == result
    puts 'succeeded in assert_equal'.magenta
  else
    puts 'failed in assert_equal'.cyan
  end
end
assert_equal(1,1)
assert_equal(1,2)

This is easy, just write it inside the function to display the input. The message just changes the wording. \ # + begin \ _src ruby> ["expected", 1]> ["result", 1]> succeeded in assert \ _equal> ["expected", 1]> ["result", 2]> failed in assert \ _equal \ #end \ _src

To be able to create multiple functions and execute them individually

Let's also create a function called assert \ _not \ _equal

def assert_not_equal(expected,result)
  p ['expected',expected]
  p ['result',result]
  if expected == result
    puts 'failed in assert_not_equal'.magenta
  else
    puts 'succeeded in assert_not_equal'.cyan
  end
end
#end_src
By the way assert_equal and assert_not_Since there are overlapping parts with equal, another function
#begin_src_ruby
def puts_vals(expected, result)
  p ['expected',expected]
  p ['result',result]
end

I want to call this from another script. That is, in another script

assert_equal(3,2)

If you write and put it, make something like that only 3 and 2 will be compared
In such a case

if $PROGRAM_NAME == __FILE__
  assert_equal(1,1)
  assert_equal(1,2)
  assert_not_equal(1,2)
  assert_not_equal(1,3)
end

Then, if the file name (FILE) of the file in which this code is written matches the moving file ($ PROGRAM \ _NAME), it will be executed. When you call this file from another file, you don't have to compare 1 and 2
Create another file

require './assert_equal'

assert_equal(3, 2)
assert_not_equal('MO_data', 'MO_date')

When executed as \ # + begin \ _src rubyexpected :: 3result :: 2failed in assert \ _equal.expected :: MO \ _dataresult :: MO \ _datesucceeded in assert \ _not \ _equal. \ # End \ _src is properly determined.

Reference article

Chart type ruby-IV (assert \ _equal)


Recommended Posts