당신은 할 수 :
class MyObject; end
def [](*args)
MyObject.new *args
end
# but you can't use it directly:
o = [] #=> [] (empty Array)
# you must to refer to self:
o = self[] #=> #<MyObject:0x1234567>
# but since self depends on where are you, you must assign self to a global variable:
$s = self
o = $s[]
# or to a constant:
S = self
o = S[]
# however, in that case it's better to do it in the proper class:
class << MyObject
def [](*args)
new *args
end
end
# and assign it to a single-letter constant to reduce characters:
S = MyObject
# so
o = S[] #=> #<MyObject:0x1234568>
좀 더 컴팩트 뭔가 생각할 수 없다.
Aww ... 나는 C/C++ preproccesor와 같은 것을 원했다. :) – itdoesntwork
@itdoesntwork 그렇다면 전처리기를 사용하라. 그러나 나는 너를 넘어서서 너를 무너 뜨리는 나무를 짖고 있다고 생각한다. 자비. –
글쎄, 나는 그 일을 할 계획이 아니었다. 그것은 가설 적이 었습니다. 나는 그것이 초래할지도 모르는 약간의 문제를 상상할 수있다. – itdoesntwork