ruby-uebungen/Assignment_037/solution/readme.md

28 lines
761 B
Markdown
Raw Normal View History

2023-05-23 09:19:31 +02:00
# Lösung: Methoden einer Klasse dynamisch erzeugen
<div style="border: 1px solid grey;"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>```ruby
def to_leet(s)
s.tr('aeiou', '43107')
end
def leet_class(clazz)
clazz.instance_methods(false).each do |m|
name = m.to_s
leet_name = to_leet(name).to_sym
puts leet_name
clazz.define_method(leet_name) do |*p|
end
end
end
class M
def hello; end
def world; end
end
leet_class(M)
m = M.new
m.h3ll0
m.w0rld
``