Last active
September 24, 2017 13:50
-
-
Save huseyin/84de42e6faee77a5c376cbd952513b89 to your computer and use it in GitHub Desktop.
Ruby Reek/Rubocop için absurd OOP örneği
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Refinements is a ... | |
module Refinements | |
refine Hash do | |
def methodize! | |
each_pair do |key, value| | |
self.class.send :define_method, key.to_s, (proc { value }) | |
end | |
end | |
end | |
end | |
# Person is a ... | |
class Person | |
attr_reader :firstname, :lastname, :nationality | |
def initialize(firstname, lastname, nation) | |
@firstname = firstname | |
@lastname = lastname | |
@nationality = Nationality.new(nation) | |
end | |
def self.create(firstname, lastname, nation) | |
new(firstname, lastname, nation) | |
end | |
def fullname | |
"#{firstname} #{lastname}" | |
end | |
def to_s | |
"#{fullname}, #{nationality}" | |
end | |
private_class_method :new | |
end | |
# Nationality is a ... | |
class Nationality | |
attr_accessor :nation | |
using Refinements | |
NATIONS = { | |
tr: 'Türkiye Cumhuriyeti', | |
en: 'İngiltere', | |
uk: 'Birleşik Krallık', | |
es: 'İspanya', | |
fr: 'Fransa' | |
}.freeze | |
def initialize(nation) | |
@nation = nation | |
NATIONS.methodize! | |
end | |
def country | |
NATIONS.send(nation) | |
end | |
def to_s | |
country | |
end | |
end | |
def ask(prompt) | |
$stderr.print prompt | |
gets.chomp.force_encoding 'utf-8' | |
end | |
if __FILE__ == $PROGRAM_NAME | |
firstname = ask 'Adınız: ' | |
lastname = ask 'Soyadınız: ' | |
person = Person.create(firstname, lastname, :tr) | |
$stderr.puts person | |
person.nationality.nation = :en | |
$stderr.puts person | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment