Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Make a class evaluate to false in boolean expressions

Reply
Thread Tools

Make a class evaluate to false in boolean expressions

 
 
Matt Margolis
Guest
Posts: n/a
 
      10-09-2007
I want to have a class that always evaluates to false in boolean
expressions. Is there a way to extend Ruby so that it evaluates an
object of a class to false like false and nil?

Example

class AlwaysFalse
end

should_be_false = AlwaysFalse.new

if should_be_false
#I don't want this to ever run
else
#will always run
end

The use case for this is to have the concept of false but have it
contain instance variables. You can't stick instance variables on
FalseClass because it is a singleton and every use of false will
overwrite the previous instance variable values.
a = false
a.name = "fish"
b = false
b.name = "ocean"
#at this point in time a.name is equal to "ocean" and not "fish" like I
want.

This example may seem like an awful design decision but in the program I
am writing it is important to have an interface like this to meet the
requirements I have to code against.

Is achieving this behavior as simple as redefining some operator methods
like == or is this sort of behavior not possible in Ruby?

Thank you,
Matt Margolis

 
Reply With Quote
 
 
 
 
Logan Capaldo
Guest
Posts: n/a
 
      10-09-2007
On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
> I want to have a class that always evaluates to false in boolean
> expressions. Is there a way to extend Ruby so that it evaluates an
> object of a class to false like false and nil?
>
> Example
>
> class AlwaysFalse
> end
>
> should_be_false = AlwaysFalse.new
>
> if should_be_false
> #I don't want this to ever run
> else
> #will always run
> end
>
> The use case for this is to have the concept of false but have it
> contain instance variables. You can't stick instance variables on
> FalseClass because it is a singleton and every use of false will
> overwrite the previous instance variable values.
> a = false
> a.name = "fish"
> b = false
> b.name = "ocean"
> #at this point in time a.name is equal to "ocean" and not "fish" like I
> want.
>
> This example may seem like an awful design decision but in the program I
> am writing it is important to have an interface like this to meet the
> requirements I have to code against.
>
> Is achieving this behavior as simple as redefining some operator methods
> like == or is this sort of behavior not possible in Ruby?
>

The latter.

OTOH:

class Object
def false?
false
end
end

class FalseClass
def false?
true
end
end

class YourClass
def false?
true
end
end

if condition.false?
...
end

Kind of annoying if you forget the .false? though. A better thing to
do would be to figure out what you are trying to model by having
multiple "falsy" values with some state.

> Thank you,
> Matt Margolis
>
>


 
Reply With Quote
 
 
 
 
Matt Margolis
Guest
Posts: n/a
 
      10-09-2007
Logan Capaldo wrote:
> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>
>> I want to have a class that always evaluates to false in boolean
>> expressions. Is there a way to extend Ruby so that it evaluates an
>> object of a class to false like false and nil?
>>
>> Example
>>
>> class AlwaysFalse
>> end
>>
>> should_be_false = AlwaysFalse.new
>>
>> if should_be_false
>> #I don't want this to ever run
>> else
>> #will always run
>> end
>>
>> The use case for this is to have the concept of false but have it
>> contain instance variables. You can't stick instance variables on
>> FalseClass because it is a singleton and every use of false will
>> overwrite the previous instance variable values.
>> a = false
>> a.name = "fish"
>> b = false
>> b.name = "ocean"
>> #at this point in time a.name is equal to "ocean" and not "fish" like I
>> want.
>>
>> This example may seem like an awful design decision but in the program I
>> am writing it is important to have an interface like this to meet the
>> requirements I have to code against.
>>
>> Is achieving this behavior as simple as redefining some operator methods
>> like == or is this sort of behavior not possible in Ruby?
>>
>>

> The latter.
>
> OTOH:
>
> class Object
> def false?
> false
> end
> end
>
> class FalseClass
> def false?
> true
> end
> end
>
> class YourClass
> def false?
> true
> end
> end
>
> if condition.false?
> ...
> end
>
> Kind of annoying if you forget the .false? though. A better thing to
> do would be to figure out what you are trying to model by having
> multiple "falsy" values with some state.
>
>
>> Thank you,
>> Matt Margolis
>>
>>
>>

>
>

I don't want to have to call .false? though. Ideally what I would like
to be able to do is just use the object in boolean expressions like
false. Anyone have any idea if this is at all possible?

Matt Margolis

 
Reply With Quote
 
Logan Capaldo
Guest
Posts: n/a
 
      10-09-2007
On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
> Logan Capaldo wrote:
> > On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
> >> Is achieving this behavior as simple as redefining some operator methods
> >> like == or is this sort of behavior not possible in Ruby?
> >>
> >>

> > The latter.
> >


Quoting myself. In other words, no you can't do it.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-09-2007
On 09.10.2007 18:12, Logan Capaldo wrote:
> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>> Logan Capaldo wrote:
>>> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>>>> Is achieving this behavior as simple as redefining some operator methods
>>>> like == or is this sort of behavior not possible in Ruby?
>>>>
>>>>
>>> The latter.
>>>

>
> Quoting myself. In other words, no you can't do it.


An alternative approach might be to use 'case' expressions instead of
'if' and implement #=== properly in that class.

Matt, what are you implementing? Why is this functionality crucial?

Kind regards

robert
 
Reply With Quote
 
John Joyce
Guest
Posts: n/a
 
      10-09-2007

On Oct 9, 2007, at 10:53 AM, Matt Margolis wrote:

> I want to have a class that always evaluates to false in boolean
> expressions. Is there a way to extend Ruby so that it evaluates an
> object of a class to false like false and nil?
>
> Example
>
> class AlwaysFalse
> end
>
> should_be_false = AlwaysFalse.new
>
> if should_be_false
> #I don't want this to ever run
> else
> #will always run
> end

This is your design problem.
You can't change the language to do what it doesn't do.
What you want is probably not a class if it always returns false.
You want a constant value.
You can create a class with an instance or class variable that
happens to be a constant. (no setter, only a getter)
so you might do

The problem is your if will go to else if the should_be_false is
false. not clear logic in the code.
Just do it the Ruby way, create a method or function:
my_class.false?
or
false?( my_arg )

This way the logic is more obvious and clear like natural language.

 
Reply With Quote
 
Matt Margolis
Guest
Posts: n/a
 
      10-09-2007
Robert Klemme wrote:
> On 09.10.2007 18:12, Logan Capaldo wrote:
>> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>>> Logan Capaldo wrote:
>>>> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>>>>> Is achieving this behavior as simple as redefining some operator
>>>>> methods
>>>>> like == or is this sort of behavior not possible in Ruby?
>>>>>
>>>>>
>>>> The latter.
>>>>

>>
>> Quoting myself. In other words, no you can't do it.

>
> An alternative approach might be to use 'case' expressions instead of
> 'if' and implement #=== properly in that class.
>
> Matt, what are you implementing? Why is this functionality crucial?
>
> Kind regards
>
> robert
>

I can't really get into specifics but basically I am doing something
like the following

module PrimitiveExtensions
attr_accessor :name
def to_extended_primitive(name)
@name = name
return self
end


I then mix this in to String, Fixnum, Float etc...

MyClass contains
def my_val=(obj)
obj.to_extended_primitive("MyVal")
end
def other_val=(obj)
obj.to_extended_primitive("OtherVal")
end

The purpose of all of this is to unify an interface so that I can store
primitive values along with a name without having to go through a
container class to get at the value.
a = MyClass.new.my_val = 7
and then later be able to go back and do something like
a.name and get back "MyVal"


This approach works for all the base types except for true and false
since they are Singletons so I can't store a @name on them.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-09-2007
On 09.10.2007 19:41, Matt Margolis wrote:
> Robert Klemme wrote:
>> On 09.10.2007 18:12, Logan Capaldo wrote:
>>> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>>>> Logan Capaldo wrote:
>>>>> On 10/9/07, Matt Margolis <(E-Mail Removed)> wrote:
>>>>>> Is achieving this behavior as simple as redefining some operator
>>>>>> methods
>>>>>> like == or is this sort of behavior not possible in Ruby?
>>>>>>
>>>>>>
>>>>> The latter.
>>>>>
>>>
>>> Quoting myself. In other words, no you can't do it.

>>
>> An alternative approach might be to use 'case' expressions instead of
>> 'if' and implement #=== properly in that class.
>>
>> Matt, what are you implementing? Why is this functionality crucial?
>>
>> Kind regards
>>
>> robert
>>

> I can't really get into specifics but basically I am doing something
> like the following
>
> module PrimitiveExtensions
> attr_accessor :name
> def to_extended_primitive(name)
> @name = name
> return self
> end
>
>
> I then mix this in to String, Fixnum, Float etc...
>
> MyClass contains
> def my_val=(obj)
> obj.to_extended_primitive("MyVal")
> end
> def other_val=(obj)
> obj.to_extended_primitive("OtherVal")
> end


This seems a strange way to use assignment since you do not modify the
receiver but the sender. I'd say this will likely lead to hard to code
that is hard to understand.

> The purpose of all of this is to unify an interface so that I can store
> primitive values along with a name without having to go through a
> container class to get at the value.
> a = MyClass.new.my_val = 7
> and then later be able to go back and do something like
> a.name and get back "MyVal"
>
> This approach works for all the base types except for true and false
> since they are Singletons so I can't store a @name on them.


I doubt this approach works for all basic types since you will get
aliasing effects with Fixnums, i.e. you set the name in one place and it
will be visible on all other places as well as Fixnums are singletons
per value.

I do not know why you are so eager to have the attribute stored in the
primitive value. Considering what I have seen of your requirements I
would probably do something like this

NamedValue = Struct.new :value, :name

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Evaluate postgres boolean field andydtaylor@gmail.com Python 3 01-05-2013 12:24 PM
Best way to evaluate boolean expressions from strings? Gustavo Narea Python 4 04-28-2009 08:09 PM
why in class Boolean, hashcode() of "true" is 1231 and of "false" is1237? junzhang1983@gmail.com Java 49 11-21-2008 03:49 AM
difference between 'boolean' and 'java.lang.Boolean' J Leonard Java 4 01-19-2008 02:56 AM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM



Advertisments