# Operator redefinition in YARV

e\$B\$^\$D\$b\$He(B e\$B\$f\$-\$R\$m\$G\$9e(B

e\$B%a%bBe\$o\$j\$G\$9!#e(B

YARVe\$B\$G\$O1i;;;R%a%=%C%I\$N:FDj5A\$K\$^[email protected]~\$7\$F\$\$\$J\$\$\$N\$G\$9\$M!#e(B
mathne\$B\$,F0\$-\$^\$;\$s!#e(B

e\$BD>\$=\$&\$H;W\$C\$?\$1\$I!"\$I\$3\$G1i;;;R\$rFCJL07\$\$\$7\$F\$k\$+\$o\$+\$j\$^\$;e(B
e\$B\$s\$G\$7\$?!#e(B

[email protected]\$G\$9!%e(B

Yukihiro M. wrote:

YARVe\$B\$G\$O1i;;;R%a%=%C%I\$N:FDj5A\$K\$^[email protected]~\$7\$F\$\$\$J\$\$\$N\$G\$9\$M!#e(B
mathne\$B\$,F0\$-\$^\$;\$s!#e(B

e\$B!!BP1~\$7\$F\$\$\$^\$9!%e(B

[Fixnum, Float, Bignum].each{|klass|
klass.class_eval{
[:+, :-, :/, :*].each{|op|
eval %{
def #{op}(a)
‘#{klass}##{op}’
end
}
}
}
}

b = 100 ** 100
p([1+2, 1-2, 1/2, 12])
p([1.1+2, 1.1-2, 1.1/2, 1.1
2])
p([b+b, b-b, b/b, b*b])
#=>
[“Fixnum#+”, “Fixnum#-”, “Fixnum#/”, “Fixnum#"]
[“Float#+”, “Float#-”, “Float#/”, "Float#
”]
[“Bignum#+”, “Bignum#-”, “Bignum#/”, “Bignum#*”]

e\$B!!B?J,!\$%P%0\$,\$"\$k\$H;W\$&\$s\$G\$9\$,!\$!J=PMh\$l\$P>.\$5\$\$!K:F8=%W%m%0%i%`\$r65\$(e(B
[email protected]\$1\$^\$;\$s\$+!%e(B

e\$B\$^\$D\$b\$He(B e\$B\$f\$-\$R\$m\$G\$9e(B

In message “Re: [ruby-dev:30745] Re: operator redefinition in YARV”
on Wed, 2 May 2007 10:55:43 +0900, SASADA Koichi [email protected]
writes:

|Yukihiro M. wrote:
|> YARVe\$B\$G\$O1i;;;R%a%=%C%I\$N:FDj5A\$K\$^[email protected]~\$7\$F\$\$\$J\$\$\$N\$G\$9\$M!#e(B
|> mathne\$B\$,F0\$-\$^\$;\$s!#e(B
|
|e\$B!!BP1~\$7\$F\$\$\$^\$9!%e(B

e\$B\$G\$9\$M\$(!#%a!<%k=P\$7\$?D>8e\$K<j85\$G;w\$?\$h\$&\$J\$3\$H\$r\$7\$?\$i\$A\$ce(B
e\$B\$s\$HF0\$\$\$A\$c\$\$\$^\$7\$?!#\$4\$a\$s\$J\$5\$\$!#e(B

|e\$B!!B?J,!\$%P%0\$,\$"\$k\$H;W\$&\$s\$G\$9\$,!\$!J=PMh\$l\$P>.\$5\$\$!K:F8=%W%m%0%i%`\$r65\$(e(B
|[email protected]\$1\$^\$;\$s\$+!%e(B

% ruby1.8 -r mathn -e ‘p (25/11)’
25/11
% ruby1.9 -r mathn -e ‘p (25/11)’
2

e\$B\$G\$9!#e(Bmathne\$B\$,Bg\$-\$\$\$s\$G\$"\$^\$j>.\$5\$/\$J\$\$:F8=%W%m%0%i%`\$G\$9\$,!#e(B

[email protected]\$G\$9!%e(B

Yukihiro M. wrote:

% ruby1.8 -r mathn -e ‘p (25/11)’
25/11
% ruby1.9 -r mathn -e ‘p (25/11)’
2

e\$B!!e(Bremove_method e\$B\$He(B alias
e\$B\$G:FDj5A%A%’%C%/\$r\$7\$F\$\$\$J\$+\$C\$?\$N\$,860x\$G\$7e(B
e\$B\$?!%D>\$7\$^\$7\$?!%e(B

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.