Last active
August 29, 2015 14:04
Wlass
This file contains 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
2.1.0 |
This file contains 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
source 'https://rubygems.org' | |
gem 'rubyfish', '~> 0.0.6' |
This file contains 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
GEM | |
remote: https://rubygems.org/ | |
specs: | |
rubyfish (0.0.6) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
rubyfish (~> 0.0.6) |
This file contains 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
require 'rubyfish' | |
# TODO: Variable handling | |
class Wlass | |
def self.closest_class_name(name) | |
classes = Module.constants.select { |c| Module.const_get(c).is_a? Class } | |
classes.find_all { |c| RubyFish::Levenshtein.distance(name, c) <= 3 }.sort_by(&:size).first | |
end | |
def self.closest_module_name(name) | |
classes = Module.constants.select { |c| Module.const_get(c).is_a? Module } | |
classes.find_all { |c| RubyFish::Levenshtein.distance(name, c) <= 3 }.sort_by(&:size).first | |
end | |
def self.closest_method_name(name) | |
ms = methods + private_methods | |
ms.find_all { |m| RubyFish::Levenshtein.distance(name, m) <= 3 }.sort_by(&:size).first | |
end | |
def self.a(original_name, superclass = Object, &class_definition) | |
name = make_class_name_from original_name | |
if !Object.const_defined?(name) | |
close_enough = closest_class_name(name) | |
if !close_enough.nil? | |
name = close_enough | |
else | |
Object.const_set(name, Class.new(superclass)) | |
end | |
end | |
klass = Object.const_get(name) | |
if !class_definition.nil? | |
klass.class_eval &class_definition | |
end | |
klass | |
end | |
def self.b(original_name, &module_definition) | |
name = make_class_name_from original_name | |
if !Object.const_defined?(name) | |
close_enough = closest_module_name(name) | |
if !close_enough.nil? | |
name = close_enough | |
else | |
Object.const_set(name, Module.new) | |
end | |
end | |
nodule = Object.const_get(name) | |
if !module_definition.nil? | |
nodule.module_eval &module_definition | |
end | |
nodule | |
end | |
def self.c(original_name) | |
method_name(original_name) | |
end | |
def self.d(original_name) | |
prefix, name = variable_name(original_name) | |
type = case prefix | |
when '$' | |
nil # TODO: Figure out global variable support | |
when '@' | |
:instance | |
when '@@' | |
:class | |
else | |
:local | |
end | |
[name, type] | |
end | |
private | |
def self.variable_name(original_name) | |
name = original_name.gsub(%r< [^a-z0-9_@$] >ix, '_').downcase | |
prefix = nil | |
name.gsub!(%r<^ ([@$]+)? >x) do | |
prefix = $1 | |
"a_" | |
end | |
[prefix, name] | |
end | |
def self.method_name(original_name) | |
name = original_name.gsub(%r< [^a-z0-9_] >ix, '_').downcase | |
name = "a_#{name}" if name.match(%r<^ [^a-z] >x) | |
if !method_defined?(name) | |
close_enough = closest_method_name(name) | |
if !close_enough.nil? | |
name = close_enough | |
end | |
end | |
name | |
end | |
def self.make_class_name_from(original_name) | |
name = original_name.gsub(%r< [^a-z0-9_] >ix, '').downcase | |
name = "a_#{name}" if name.match(%r<^ [^a-z] >x) | |
name.capitalize | |
end | |
end | |
module Kernel | |
def W(*args, &class_definition) | |
Wlass.a(*args, &class_definition) | |
end | |
def WM(*args, &module_definition) | |
Wlass.b(*args, &module_definition) | |
end | |
def MM(original_name, actual_method_name) | |
new_name = Wlass.c(original_name) | |
binding.eval <<-EOS | |
alias_method :#{new_name}, :#{actual_method_name} | |
EOS | |
end | |
def M(actual_name, *args, &block) | |
send(Wlass.c(actual_name), *args, &block) | |
end | |
W("Not defined, and please never define it. ZRRRK") do | |
end | |
def V(actual_name, contents = W("Not defined, and please never define it. ZRRRK")) | |
name, type = Wlass.d(actual_name) | |
if contents.instance_of?(W("Not defined, and please never define it. ZRRRK").class) | |
case type | |
when :local | |
# TODO: not sure if possible | |
when :instance | |
instance_variable_get("@#{name}".to_sym) | |
when :class | |
end | |
else | |
case type | |
when :local | |
# TODO: not sure if possible | |
when :instance | |
instance_variable_set("@#{name}".to_sym, contents) | |
when :class | |
end | |
end | |
end | |
end | |
W("Print Hello World") do | |
def initialize | |
puts "Hello World" | |
end | |
end | |
W("Print Hello World") do | |
private | |
def goodbye | |
puts "Goodbye World" | |
end | |
end | |
world = W("print hello world!").new | |
class Something < W("Print hello world") | |
def initialize | |
puts "Something!" | |
end | |
end | |
something = Something.new | |
W("New kind of Something", W("Something")) do | |
def initialize | |
puts "New kind of Something!" | |
end | |
end | |
W("new kind of something").new | |
WM("My fine module") do | |
def say_hello | |
puts "Hello from the module" | |
end | |
end | |
W("With a module") do | |
include WM("my fine modulee") | |
end | |
vroom = W("with a module").new | |
vroom.say_hello | |
W("This is")::W("A namespace") do | |
def initialize | |
puts "Look, namespaces!" | |
end | |
end | |
W("this is")::W("a namespace").new | |
W("A nice class with methods") do | |
MM "another method", def _a2 | |
puts "Hi from the other method!" | |
end | |
MM "print pi and e and a name", def _a1(greeting:) | |
puts "Hi #{yield}, #{greeting}. π is #{Math::PI} and e is #{Math::E}" | |
V("@a variable", "with contents") | |
puts V("@a variable") | |
M("another method") | |
end | |
end | |
W("a nice class with methods").new.M("print pi and e and a name", greeting: "how are you?") do | |
"Kim" | |
end | |
# Doesn't work since we're not in the same context as in the method above | |
# TODO: look at #caller_locations to solve this? | |
puts W("a nice class with methods").V("@a variable") | |
W("A difficult to spell class") do | |
MM "a method", def _ | |
puts "OMG!" | |
end | |
MM "another method", def _ | |
puts "another OMG!" | |
end | |
end | |
W("a dificult to spell klass").new.M("a method") | |
W("a dificult to spell klass").new.M("another method") | |
W("a dificult to spell klass").new.M("a method") | |
W("a dificult to spell klass").new.M("e nethod") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
⚡ 💩