* [ruby-dev:48593] [ruby-trunk - Feature #10327] [Open] Bool/False/True module for '==='
[not found] <redmine.issue-10327.20141005160917@ruby-lang.org>
@ 2014-10-05 16:09 ` arima.yasuhiro
2014-10-13 10:05 ` [ruby-dev:48623] [ruby-trunk - Feature #10327] " zn
2014-10-15 14:42 ` [ruby-dev:48640] " arima.yasuhiro
2 siblings, 0 replies; 3+ messages in thread
From: arima.yasuhiro @ 2014-10-05 16:09 UTC (permalink / raw
To: ruby-dev
Issue #10327 has been reported by yasuhiro arima.
----------------------------------------
Feature #10327: Bool/False/True module for '==='
https://bugs.ruby-lang.org/issues/10327
* Author: yasuhiro arima
* Status: Open
* Priority: Normal
* Assignee:
* Category: core
* Target version:
----------------------------------------
ruby-core:0237 から始まる Boolean Class の話題で Boolean Module が提案されていました。
それは必要とされているのか?という疑問とともに .true? メソッドなどが議論されていました。
それを読んで、=== メソッドを利用する Bool, False, True Module を書いてみました。
これを使うと case 式で when TrueClass, FalseClass ではなく when Bool と短く書けます。
また、case 式で 偽は when nil, false ではなく when False と書けます。
そして、真は else ではなく when True と書けます。
Bool, False, True という名前にしたのは、短く書きたいことと、リファレンスマニュアルでも使われているためです。
以下の ruby コードとほぼ同じ内容を C で書いた patch を添えます。
~~~ruby
module Bool
def self.append_features(m)
if ((m != FalseClass) and (m != TrueClass))
raise TypeError, "`#{m.name}' isn't FalseClass nor TrueClass", caller(1)
end
super
end
end
class FalseClass
include Bool
end
class TrueClass
include Bool
end
module False
def self.===(other)
other.nil? || other == false
end
end
module True
def self.===(other)
!other.nil? && other != false
end
end
~~~
以下が実行例です。
~~~
$ ./miniruby -e 'class String; include Bool; end;'
-e:1:in `append_features': 'String' isn't FalseClass nor TrueClass (TypeError)
from -e:1:in `include'
from -e:1:in `<class:String>'
from -e:1:in `<main>'
$ ./miniruby -e '[nil, false, true, 0].each {|obj| p obj.class.ancestors}'
[NilClass, Object, Kernel, BasicObject]
[FalseClass, Bool, Object, Kernel, BasicObject]
[TrueClass, Bool, Object, Kernel, BasicObject]
[Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when Bool; p [obj, Bool]
else p [obj, "-"]
end
}'
[nil, "-"]
[false, Bool]
[true, Bool]
[0, "-"]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when False; p [obj, false]
when True; p [obj, true]
else p [obj, "-"]
end
}'
[nil, false]
[false, false]
[true, true]
[0, true]
~~~
---Files--------------------------------
bool-false-true.patch (3.44 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-dev:48623] [ruby-trunk - Feature #10327] Bool/False/True module for '==='
[not found] <redmine.issue-10327.20141005160917@ruby-lang.org>
2014-10-05 16:09 ` [ruby-dev:48593] [ruby-trunk - Feature #10327] [Open] Bool/False/True module for '===' arima.yasuhiro
@ 2014-10-13 10:05 ` zn
2014-10-15 14:42 ` [ruby-dev:48640] " arima.yasuhiro
2 siblings, 0 replies; 3+ messages in thread
From: zn @ 2014-10-13 10:05 UTC (permalink / raw
To: ruby-dev
Issue #10327 has been updated by Kazuhiro NISHIYAMA.
nil も含むなら False/True よりも Falsy/Truthy の方が良いのではないでしょうか。
case で使うだけなら `:!.to_proc` と `:itself.to_proc` を使うという方法もありそうです。
(`itself` は 2.2 以降ですが。)
~~~
ruby -e '[nil, false, true, 0].each do |obj|
case obj
when :!.to_proc; p [obj, false]
when :itself.to_proc; p [obj, true]
else p [obj, "-"]
end
end'
[nil, false]
[false, false]
[true, true]
[0, true]
~~~
----------------------------------------
Feature #10327: Bool/False/True module for '==='
https://bugs.ruby-lang.org/issues/10327#change-49395
* Author: yasuhiro arima
* Status: Open
* Priority: Normal
* Assignee:
* Category: core
* Target version:
----------------------------------------
ruby-core:0237 から始まる Boolean Class の話題で Boolean Module が提案されていました。
それは必要とされているのか?という疑問とともに .true? メソッドなどが議論されていました。
それを読んで、=== メソッドを利用する Bool, False, True Module を書いてみました。
これを使うと case 式で when TrueClass, FalseClass ではなく when Bool と短く書けます。
また、case 式で 偽は when nil, false ではなく when False と書けます。
そして、真は else ではなく when True と書けます。
Bool, False, True という名前にしたのは、短く書きたいことと、リファレンスマニュアルでも使われているためです。
以下の ruby コードとほぼ同じ内容を C で書いた patch を添えます。
~~~ruby
module Bool
def self.append_features(m)
if ((m != FalseClass) and (m != TrueClass))
raise TypeError, "`#{m.name}' isn't FalseClass nor TrueClass", caller(1)
end
super
end
end
class FalseClass
include Bool
end
class TrueClass
include Bool
end
module False
def self.===(other)
other.nil? || other == false
end
end
module True
def self.===(other)
!other.nil? && other != false
end
end
~~~
以下が実行例です。
~~~
$ ./miniruby -e 'class String; include Bool; end;'
-e:1:in `append_features': 'String' isn't FalseClass nor TrueClass (TypeError)
from -e:1:in `include'
from -e:1:in `<class:String>'
from -e:1:in `<main>'
$ ./miniruby -e '[nil, false, true, 0].each {|obj| p obj.class.ancestors}'
[NilClass, Object, Kernel, BasicObject]
[FalseClass, Bool, Object, Kernel, BasicObject]
[TrueClass, Bool, Object, Kernel, BasicObject]
[Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when Bool; p [obj, Bool]
else p [obj, "-"]
end
}'
[nil, "-"]
[false, Bool]
[true, Bool]
[0, "-"]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when False; p [obj, false]
when True; p [obj, true]
else p [obj, "-"]
end
}'
[nil, false]
[false, false]
[true, true]
[0, true]
~~~
---Files--------------------------------
bool-false-true.patch (3.44 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-dev:48640] [ruby-trunk - Feature #10327] Bool/False/True module for '==='
[not found] <redmine.issue-10327.20141005160917@ruby-lang.org>
2014-10-05 16:09 ` [ruby-dev:48593] [ruby-trunk - Feature #10327] [Open] Bool/False/True module for '===' arima.yasuhiro
2014-10-13 10:05 ` [ruby-dev:48623] [ruby-trunk - Feature #10327] " zn
@ 2014-10-15 14:42 ` arima.yasuhiro
2 siblings, 0 replies; 3+ messages in thread
From: arima.yasuhiro @ 2014-10-15 14:42 UTC (permalink / raw
To: ruby-dev
Issue #10327 has been updated by yasuhiro arima.
File bool-falsy-truthy.patch added
もっともなので Falsy/Truthy に変えてみました。
patch を作り直したので添付します。
前回の patch は壊れていました。確認が不十分でごめんなさい。
~~~
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when Falsy; p [obj, false]
when Truthy; p [obj, true]
else p [obj, "-"]
end
}'
[nil, false]
[false, false]
[true, true]
[0, true]
~~~
また、to_proc を使う案ですが、短く書きたいのが主題なのでそれでは嬉しくないのです。
----------------------------------------
Feature #10327: Bool/False/True module for '==='
https://bugs.ruby-lang.org/issues/10327#change-49466
* Author: yasuhiro arima
* Status: Open
* Priority: Normal
* Assignee:
* Category: core
* Target version:
----------------------------------------
ruby-core:0237 から始まる Boolean Class の話題で Boolean Module が提案されていました。
それは必要とされているのか?という疑問とともに .true? メソッドなどが議論されていました。
それを読んで、=== メソッドを利用する Bool, False, True Module を書いてみました。
これを使うと case 式で when TrueClass, FalseClass ではなく when Bool と短く書けます。
また、case 式で 偽は when nil, false ではなく when False と書けます。
そして、真は else ではなく when True と書けます。
Bool, False, True という名前にしたのは、短く書きたいことと、リファレンスマニュアルでも使われているためです。
以下の ruby コードとほぼ同じ内容を C で書いた patch を添えます。
~~~ruby
module Bool
def self.append_features(m)
if ((m != FalseClass) and (m != TrueClass))
raise TypeError, "`#{m.name}' isn't FalseClass nor TrueClass", caller(1)
end
super
end
end
class FalseClass
include Bool
end
class TrueClass
include Bool
end
module False
def self.===(other)
other.nil? || other == false
end
end
module True
def self.===(other)
!other.nil? && other != false
end
end
~~~
以下が実行例です。
~~~
$ ./miniruby -e 'class String; include Bool; end;'
-e:1:in `append_features': 'String' isn't FalseClass nor TrueClass (TypeError)
from -e:1:in `include'
from -e:1:in `<class:String>'
from -e:1:in `<main>'
$ ./miniruby -e '[nil, false, true, 0].each {|obj| p obj.class.ancestors}'
[NilClass, Object, Kernel, BasicObject]
[FalseClass, Bool, Object, Kernel, BasicObject]
[TrueClass, Bool, Object, Kernel, BasicObject]
[Fixnum, Integer, Numeric, Comparable, Object, Kernel, BasicObject]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when Bool; p [obj, Bool]
else p [obj, "-"]
end
}'
[nil, "-"]
[false, Bool]
[true, Bool]
[0, "-"]
$ ./miniruby -e '[nil, false, true, 0].each {|obj|
case obj
when False; p [obj, false]
when True; p [obj, true]
else p [obj, "-"]
end
}'
[nil, false]
[false, false]
[true, true]
[0, true]
~~~
---Files--------------------------------
bool-false-true.patch (3.44 KB)
bool-falsy-truthy.patch (3.85 KB)
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2014-10-15 14:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <redmine.issue-10327.20141005160917@ruby-lang.org>
2014-10-05 16:09 ` [ruby-dev:48593] [ruby-trunk - Feature #10327] [Open] Bool/False/True module for '===' arima.yasuhiro
2014-10-13 10:05 ` [ruby-dev:48623] [ruby-trunk - Feature #10327] " zn
2014-10-15 14:42 ` [ruby-dev:48640] " arima.yasuhiro
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).