From: Kenneth on 7 Jul 2010 09:15 I see. Thanks! -- Posted via http://www.ruby-forum.com/.
From: Brian Candler on 7 Jul 2010 09:16 user = Object.new def user.banned?; false; end def user.activated?; true; end # Example 3 class Pred def initialize(sym) @sym = sym end def ===(other) other.send(@sym) end end def is(sym); Pred.new(sym); end Banned = is(:banned?) Activated = is(:activated?) case user when Banned; puts "Banned!" when Activated; puts "Activated!" end -- Posted via http://www.ruby-forum.com/.
From: Martin DeMello on 7 Jul 2010 10:18 On Wed, Jul 7, 2010 at 6:23 PM, Kenneth <ken70r(a)gmail.com> wrote: > I was hoping there would be something like > case user > when banned? then ... > when activated? then ... > end > > I would like to see something like this but it seems hard to implement. class User attr_accessor :banned def banned? banned end def active? not banned end end class Symbol def ===(other) other.send(self) end end a = User.new a.banned = true case a when :banned? ; puts "banned!" when :active? ; puts "active!" end a.banned = false case a when :banned? ; puts "banned!" when :active? ; puts "active!" end martin
From: Benoit Daloze on 7 Jul 2010 13:01 On 7 July 2010 17:18, Martin DeMello <martindemello(a)gmail.com> wrote: > class User > attr_accessor :banned > > def banned? > banned > end > > def active? > not banned > end > end > > class Symbol > def ===(other) > other.send(self) > end > end > > a = User.new > a.banned = true > > case a > when :banned? ; puts "banned!" > when :active? ; puts "active!" > end > > a.banned = false > > case a > when :banned? ; puts "banned!" > when :active? ; puts "active!" > end > > martin > Quite cool the Symbol#===, it made me think to Proc#=== Sadly, Proc need always a method to be created (so &:sym can not be used directly) But I found a (convoluted) example to use Proc#=== #encoding: utf-8 alias :ë :lambda def check(user) case user when ë(&:banned?) puts "Banned !" when ë(&:active?) puts "This is over-convoluted, but you're fine!" end end user = User.new user.banned = true check user user.banned = false check user
From: Benoit Daloze on 7 Jul 2010 13:14 On 7 July 2010 15:53, Kenneth <ken70r(a)gmail.com> wrote: > Hi, the point I was trying to make was sending a boolean method to the > case. > > Let's say I have a User object and it has instance methods banned? and > activated? They are either true or false. > > Instead of doing > case > when user.banned? then ... > when user.activated? then ... > end > > I was hoping there would be something like > case user > when banned? then ... > when activated? then ... > end > > I would like to see something like this but it seems hard to implement. Ah, and if you would absolutely respect the same syntax, it is possible without changing self (but certainly not a good practice) def method_missing(meth, *a, &b) lambda { |obj| obj.send(meth, *a, &b) } end case user when banned? puts "Banned !" when activated? puts "This is insane but you're fine!" end
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: rdoc 2.5.9 Released Next: Global methods in Ruby standard lib |