From: Kenneth     on
Let's say I have something like this

case
when "".empty? then puts "Empty"
when "".nil? then puts "Nil"
when "".include?("a") then puts "Includes 'a'"
end

Is there a way to do something like this instead? (This does not work
since the methods in the switches are acting on Object instead of "".)

case ""
when empty? then puts "Empty"
when nil? then puts "Nil"
when include?("a") then puts "Includes 'a'"
end
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
Kenneth     wrote:
> Let's say I have something like this
>
> case
> when "".empty? then puts "Empty"
> when "".nil? then puts "Nil"
> when "".include?("a") then puts "Includes 'a'"
> end
>
> Is there a way to do something like this instead? (This does not work
> since the methods in the switches are acting on Object instead of "".)
>
> case ""
> when empty? then puts "Empty"
> when nil? then puts "Nil"
> when include?("a") then puts "Includes 'a'"
> end

case str
when "": puts "Empty"
when nil: puts "Nil"
when /a/: puts "Includes 'a'"
end

The case statement actually expands to the === operator, so this is
equivalent to

if "" === str
puts "Empty"
elsif nil === str
puts "Nil"
elsif /a/ === str
puts "Includes 'a'"
end

The === operator does the "right thing" in each of these cases.
--
Posted via http://www.ruby-forum.com/.

From: Kenneth     on
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.
--
Posted via http://www.ruby-forum.com/.

From: Kenneth     on
I guess you can define a new method inside the class like this when
possible, as long as your then statements are nice.

class User
def status
case
when banned? then ...
when activated? then ...
end
end
end
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
Kenneth     wrote:
> Hi, the point I was trying to make was sending a boolean method to the
> case.

Then no, there is not a variant of the case statement which does this.

> I was hoping there would be something like
> case user
> when banned? then ...
> when activated? then ...
> end

If you want to invoke method 'banned?' on a particular object, then you
have to send it to that object. A bare called to 'banned?' is invoked on
the current object (self).

But here are a couple of alternatives to consider:

user = Object.new
def user.banned?; false; end
def user.activated?; true; end

# Example 1

user.instance_eval {
case
when banned?; puts "Banned!"
when activated?; puts "Activated!"
end
}

# Example 2

[
[:banned?, lambda { puts "Banned!" }],
[:activated?, lambda { puts "Activated!" }],
].each do |method, action|
user.send(method) && (action[]; break)
end

However if it's only a handful of conditions I'd be inclined to write

when user.banned?
when user.activated?
--
Posted via http://www.ruby-forum.com/.