Skip to content

Instantly share code, notes, and snippets.

@hachi8833
Last active May 2, 2018 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hachi8833/8229e2c43aee40e651914e9da4fe21af to your computer and use it in GitHub Desktop.
Save hachi8833/8229e2c43aee40e651914e9da4fe21af to your computer and use it in GitHub Desktop.
The Ruby code what I see beauty in
# Sandi Metz「オブジェクト指向設計実践ガイド」でリファクタリング後のduck-typingコードを美しいと思い
# それに触発されて書いたHello worldです。
# なおこのコードはRubyとGobyどちらでも動きます。
# Greetクラス
class Greet
attr_accessor :audience, :head, :tail
def initialize
@head = 'Hello, '
@tail = '!'
end
def name
audience.name
end
def say
puts head + name + tail
end
end
# MyNameモジュールでクラス名取得を共通化
module MyName
attr_reader :name
def initialize
@name = self.class.to_s
end
end
#----------
# 利用例: Worldクラス
class World
include MyName
end
greet = Greet.new
greet.audience = World.new
greet.say
# 利用例: Rubyクラス
class Ruby
include MyName
end
greet.audience = Ruby.new
greet.say
# 利用例: Gobyクラス
class Goby
include MyName
end
greet.audience = Goby.new
greet.say
@hachi8833
Copy link
Author

hachi8833 commented May 1, 2018

$ rubocop hello_world.rb
Inspecting 1 file
C

Offenses:

hello_world.rb:1:13: C: Style/AsciiComments: Use only ascii symbols in comments.
# Sandi Metz「オブジェクト指向設計実践ガイド」でリファクタリング後のduck-typingコードを美しいと思い、
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
hello_world.rb:2:3: C: Style/AsciiComments: Use only ascii symbols in comments.
# それに触発されて書いたHello worldです。
  ^^^^^^^^^^^
hello_world.rb:3:3: C: Style/AsciiComments: Use only ascii symbols in comments.
# なおこのコードはRubyとGobyどちらでも動きます。
  ^^^^^^^^
hello_world.rb:5:8: C: Style/AsciiComments: Use only ascii symbols in comments.
# Greetクラス
       ^^^
hello_world.rb:23:9: C: Style/AsciiComments: Use only ascii symbols in comments.
# MyNameモジュールでクラス名取得を共通化
        ^^^^^^^^^^^^^^^^
hello_world.rb:33:3: C: Style/AsciiComments: Use only ascii symbols in comments.
# 利用例: Worldクラス
  ^^^
hello_world.rb:42:3: C: Style/AsciiComments: Use only ascii symbols in comments.
# 利用例: Rubyクラス
  ^^^
hello_world.rb:50:3: C: Style/AsciiComments: Use only ascii symbols in comments.
# 利用例: Gobyクラス
  ^^^

1 file inspected, 8 offenses detected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment