Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

proper object implementation (for a glimps at where we are heading)

  • Loading branch information...
commit 166a5a0e0a24a7a2656a95c710f61e8e935f7171 1 parent bfc568e
@rkh authored
Showing with 147 additions and 0 deletions.
  1. +120 −0 kernel/Object.st
  2. +4 −0 lib/reak.rb
  3. +23 −0 lib/smalltalk/object.rb
View
120 kernel/Object.st
@@ -0,0 +1,120 @@
+Object extend [
+ <category: 'Kernel'>
+
+ = anObject [
+ <category: 'comparing'>
+ ^ self rubyPerform: #== with: anObject
+ ]
+
+ == anObject [
+ <category: 'comparing'>
+ ^ self rubyPerform: #'equal?' with: anObject
+ ]
+
+ ~= anObject [
+ <category: 'comparing'>
+ ^ (self = anObject) not
+ ]
+
+ ~~ anObject [
+ <category: 'comparing'>
+ ^ (self == anObject) not
+ ]
+
+ class [
+ <category: 'class membership'>
+ ^ self rubyPerform: #class
+ ]
+
+ copy [
+ <category: 'copying'>
+ ^ self rubyPerform: #dup
+ ]
+
+ doesNotUnderstand: aMessage [
+ <category: 'error handling'>
+ self error: self printString, ' does not understand message ', aMessage selector
+ ]
+
+ error: aString [
+ <category: 'error handling'>
+ self rubyPerform: #raise with: (aString rubyPerform: #'to_s')
+ ]
+
+ hash [
+ <category: 'comparing'>
+ ^ self rubyPerform: #hash
+ ]
+
+ identityHash [
+ <category: 'comparing'>
+ ^ self rubyPerform: #'object_id'
+ ]
+
+ isKindOf: aClass [
+ <category: 'class membership'>
+ ^ self rubyPerform: #'kind_of?' with: aClass
+ ]
+
+ isMemberOf: aClass [
+ <category: 'class membership'>
+ ^ self class = aClass
+ ]
+
+ isNil [
+ <category: 'testing'>
+ ^ false
+ ]
+
+ notNil [
+ <category: 'testing'>
+ ^ self isNil not
+ ]
+
+ perform: aSymbol [
+ <category: 'message handling'>
+ ^ self rubyPerform: #'send_reak' with: aSymbol
+ ]
+
+ perform: aSymbol with: anObject [
+ <category: 'message handling'>
+ ^ self rubyPerform: #'send_reak' with: aSymbol with: anObject
+ ]
+
+ perform: aSymbol with: firstObject with: secondObject [
+ <category: 'message handling'>
+ ^ self rubyPerform: #'send_reak' with: aSymbol with: firstObject with: secondObject
+ ]
+
+ perform: aSymbol with: firstObject with: secondObject with: thirdObject [
+ <category: 'message handling'>
+ ^ self rubyPerform: #'send_reak' with: aSymbol with: firstObject with: secondObject with: thirdObject
+ ]
+
+ perform: aSymbol withArguments: anArray [
+ <category: 'message handling'>
+ ^ self rubyPerform: #'send_reak' withArguments: { aSymbol }, anArray asArray
+ ]
+
+ printOn: aStream [
+ <category: 'printing'>
+ self shouldBeImplemented
+ ]
+
+ printString [
+ <category: 'printing'>
+ (#($A $E $I $O $U) includes: self class name first)
+ ifTrue: [ ^ 'an ', self class name ]
+ ifFalse: [ ^ 'a ', self class name ]
+ ]
+
+ respondsTo: aSymbol [
+ <category: 'class membership'>
+ ^ self rubyPerform: #'respond_to_reak?' with: aSymbol
+ ]
+
+ yourself [
+ <category: 'accessing'>
+ ^ self
+ ]
+]
View
4 lib/reak.rb
@@ -8,4 +8,8 @@ module Reak
autoload :Parser, 'reak/parser'
autoload :Script, 'reak/script'
autoload :Syntax, 'reak/syntax'
+
+ def self.prefixed_method(name)
+ "reak:#{name}"
+ end
end
View
23 lib/smalltalk/object.rb
@@ -0,0 +1,23 @@
+module Smalltalk
+ Object = ::Object unless const_defined? :Object
+ class Object
+ def send_reak(method, *args)
+ __send__ Reak.prefixed_method(method), *args
+ end
+
+ def send_splat(method, args)
+ __send__ method, *args.to_a
+ end
+
+ def respond_to_reak?(method)
+ respond_to? Reak.prefixed_method(method)
+ end
+
+ 0.upto(4) do |i|
+ name = 'rubyPerform:' << ('with:'*i)
+ alias_method Reak.prefixed_method(name), '__send__'
+ end
+
+ alias_method Reak.prefixed_method('rubyPerform:withArgs:'), 'send_splat'
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.