Bug #1792
Fixnum#& 等が、Rational などを受けつける
| Status: | Assigned | Start date: | 07/19/2009 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | % Done: | 0% |
||
| Category: | - | |||
| Target version: | 2.0.0 | |||
| ruby -v: | - |
Description
1 & 1.5 がエラーになるので、
1 & Rational(3,2)
や
1 & BigDecimal('1.5')
もエラーにすべきかもしれません。
Related issues
Associated revisions
* numeric.c (bit_coerce): A Fixnum and a Bignum are only permitted for
bitwise arithmetic with a Fixnum. #1792
* test/ruby/test_fixnum.rb: add tests for the above change.
* bignum.c (bit_coerce): A Fixnum and a Bignum are only permitted for
bitwise arithmetic with a Bignum. #1792
* test/ruby/test_bignum.rb: add tests for the above change.
History
Updated by ujihisa (ujihisa .) over 2 years ago
- Status changed from Open to Assigned
- Assignee set to matz (Yukihiro Matsumoto)
Updated by ko1 (Koichi Sasada) 12 months ago
これはどうしましょうか.
Updated by matz (Yukihiro Matsumoto) 12 months ago
- ruby -v changed from ruby 1.9.2dev (2009-07-19 trunk 24205) [i686-linux] to -
Updated by mrkn (Kenta Murata) 9 months ago
これは 1.9.3 にバックポートしても良いですか?
Updated by kosaki (Motohiro KOSAKI) 9 months ago
> これは 1.9.3 にバックポートしても良いですか?
以下はどのくら深刻なのでしょうか。
> 1 & 1.5 がエラーになるので、
> 1 & Rational(3,2)
> や
> 1 & BigDecimal('1.5')
> もエラーにすべきかもしれません。
freeze 後に仕様変更的な修正を入れるのは可能なら避けたいという思いがあります。
もし仮に修正漏れがあり、1.9.4で再fixとなると、エンドユーザ視点では1.9.2, 1.9.3, 1.9.4で
それぞれ微妙に挙動が異なることになり迷惑だからです。
しかし、1) 修正に自信がある、または 2) 重要度的に外せれない ということであれば反対はしません。
Updated by mrkn (Kenta Murata) 9 months ago
- Status changed from Assigned to Closed
むらたです。
そこまで重要ではないと思うので、バックポートはしないことにします。
Updated by naruse (Yui NARUSE) 9 months ago
- Status changed from Closed to Assigned
- Assignee changed from matz (Yukihiro Matsumoto) to mrkn (Kenta Murata)
これの影響で以下のようなエラーが出ています。
2) Error:
test_num2long(TestNumeric):
TypeError: can't convert Object into Integer for bitwise arithmetic
/home/naruse/ruby/test/ruby/test_numeric.rb:227:in `&'
/home/naruse/ruby/test/ruby/test_numeric.rb:227:in `test_num2long'
Updated by mrkn (Kenta Murata) 9 months ago
- Status changed from Assigned to Closed
ごめんなさいごめんなさい。
r33113 で修正しました。
Updated by naruse (Yui NARUSE) 9 months ago
散発的になってすいませんが、RubySpecも追従よろしくお願いします
Updated by ko1 (Koichi Sasada) 8 months ago
Is it intentional?
###
class IntegerMimic
def initialize n
@n = n
end
def to_int
@n
end
end
obj = IntegerMimic.new(10)
%w(& | ^).each{|sym|
p sym
begin
p(3.send(sym, obj))
rescue => e
p e
end
}
#=>
ruby 1.9.4dev (2011-09-11 trunk 33248) [i386-mswin32_100]
"&"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"|"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"^"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
(2011/08/29 7:25), Kenta Murata wrote:
>
> Issue #1792 has been updated by Kenta Murata.
>
> Status changed from Assigned to Closed
>
> ごめんなさいごめんなさい。
> r33113 で修正しました。
>
> ----------------------------------------
> Bug #1792: Fixnum#& 等が、Rational などを受けつける
> http://redmine.ruby-lang.org/issues/1792
>
> Author: tadayoshi funaba
> Status: Closed
> Priority: Normal
> Assignee: Kenta Murata
> Category:
> Target version: 1.9.x
> ruby -v: -
>
>
> =begin
> 1 & 1.5 がエラーになるので、
> 1 & Rational(3,2)
> や
> 1 & BigDecimal('1.5')
> もエラーにすべきかもしれません。
> =end
>
>
>
--
// SASADA Koichi at atdot dot net
Updated by ko1 (Koichi Sasada) 8 months ago
I find out that bitwise operation doesn't call coerce.
How to make mimic Integer class?
###
class IntegerMimic
def initialize n
@n = n
end
def to_int
p :to_int
@n
end
def coerce obj
p :coerce
[obj, @n]
end
end
obj = IntegerMimic.new(10)
%w(+ - * / % & | ^).each{|sym|
p sym
begin
p(3.send(sym, obj))
rescue => e
p e
end
}
__END__
#=>
ruby 1.9.4dev (2011-09-11 trunk 33248) [i386-mswin32_100]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"|"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
"^"
#<TypeError: can't convert IntegerMimic into Integer for bitwise arithmetic>
#=>
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
"+"
:coerce
13
"-"
:coerce
-7
"*"
:coerce
30
"/"
:coerce
0
"%"
:coerce
3
"&"
:to_int
2
"|"
:to_int
11
"^"
:to_int
9
--
// SASADA Koichi at atdot dot net
Updated by cjheath (Clifford Heath) 8 months ago
On 13/09/2011, at 9:27 AM, SASADA Koichi wrote:
> I find out that bitwise operation doesn't call coerce.
> How to make mimic Integer class?
The short answer: You can't.
The long answer:
You can imitate some of the behaviour of Integers, but when you
get into hashing and numeric operations, the different Ruby
interpreters all have different shortcuts that prevent your custom
code from being called.
See this post for more information:
<http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html>
And a discussion thread that I started:
<http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/381087>
Clifford Heath.
Updated by mrkn (Kenta Murata) 8 months ago
- Status changed from Closed to Open
I reopen this ticket because I rethink [ruby-core:39491] is bug.
But, unfortunately, I don't know how to fix this.
Using to_int cannot fix the original problem.
May Numeric#coerce is used for bitwise operation?
Updated by matz (Yukihiro Matsumoto) 8 months ago
Hi,
In message "Re: [ruby-core:39540] [Ruby 1.9 - Bug #1792][Open] Fixnum#& 等が、Rational などを受けつける"
on Wed, 14 Sep 2011 13:28:57 +0900, Kenta Murata <muraken@gmail.com> writes:
|I reopen this ticket because I rethink [ruby-core:39491] is bug.
|But, unfortunately, I don't know how to fix this.
|Using to_int cannot fix the original problem.
|May Numeric#coerce is used for bitwise operation?
Using coerce is OK for me. But it should cause NoMethodError when
implemented naively. That might cause confusion.
matz.
Updated by shyouhei (Shyouhei Urabe) 2 months ago
- Status changed from Open to Assigned