4
1

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.

マルチスケール特論 第9回

Last updated at Posted at 2020-11-19

ruby-2.7.1p83

関数で自身の名前を取得する

__method__

で取得できる

example

# frozen_string_literal: true

require 'colorize'

def assert_equal(expected, result)
  puts "expected: #{expected}"
  puts "result:   #{result}"
  if expected == result
    puts "succeeded in #{__method__}.".green
  else
    puts "failed in #{__method__}.".red
  end
end

def assert_not_equal(expected, result)
  puts "expected: #{expected}"
  puts "result:   #{result}"
  if expected != result
    puts "succeeded in #{__method__}.".green
  else
    puts "failed in #{__method__}.".red
  end
end

assert_equal(1, 1)
assert_not_equal(1, 2)

result

% ruby assert_equal.rb
expected: 1
result:   1
succeeded in assert_equal.
expected: 1
result:   2
succeeded in assert_not_equal.

org でコードを貼り付ける

今までは#+begin_src と #end_src の中身にコードを直書きしていた。が、#+include を使えば、どっかのタイミング(予想では qiita_org で org から md を生成するとき)でファイルの中身を展開してくれるらしい。ただし、ファイルの先頭と末尾に #+begin_src ruby#+end_src を記述する必要がある。

# frozen_string_literal: true

require 'colorize'

def print_expected_and_result(expected, result)
  puts "expected: #{expected}"
  puts "result:   #{result}"
end

def assert_equal(expected, result)
  print_expected_and_result(expected, result)
  if expected == result
    puts "succeeded in #{__method__}.".green
  else
    puts "failed in #{__method__}.".red
  end
end

def assert_not_equal(expected, result)
  print_expected_and_result(expected, result)
  if expected != result
    puts "succeeded in #{__method__}.".green
  else
    puts "failed in #{__method__}.".red
  end
end

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

Rubular

https://rubular.com/ a Ruby regular expression editor正規表現、よく使うけど毎回よくわからなくなるから、interactive に結果を返してくれるのは試行錯誤がとてもやりやすいので嬉しい。

scan と match

ref: 【Ruby】scanとmatchの違い


  • source ~/ghq/github.com/TeamNishitani/grad_members_20f/members/syasin-5d/9th.org
4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?