Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > determining whether an object is an immediate?

Reply
Thread Tools

determining whether an object is an immediate?

 
 
Eric Mahurin
Guest
Posts: n/a
 
      10-29-2005
This is be best I could come up with for determining whether an
object is an immediate (Fixnum, Symbol, false, true, nil,
others?):

begin
obj.clone
# clone worked - not an immediate
rescue TypeError
# clone failed - immediate
end

Anybody have a better way? If the above is the best, seems
like an Object#immediate? would be nice to have.




=09
__________________________________=20
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com


 
Reply With Quote
 
 
 
 
Timothy Hunter
Guest
Posts: n/a
 
      10-29-2005
Eric Mahurin wrote:
> This is be best I could come up with for determining whether an
> object is an immediate (Fixnum, Symbol, false, true, nil,
> others?):
>
> begin
> obj.clone
> # clone worked - not an immediate
> rescue TypeError
> # clone failed - immediate
> end
>
> Anybody have a better way? If the above is the best, seems
> like an Object#immediate? would be nice to have.
>

I seem to remember that begin/rescue is extremely slow, so since we know
up front what all the immediate classes are I guessed that it would be
faster just to test for them in a case expression. I put together a
benchmark test that confirms my suspicion. Results first, then the test.
I ran the test on my Powerbook.

me$ ruby test.rb
user system total real
rescue 2.040000 0.130000 2.170000 ( 2.52711
case 0.220000 0.010000 0.230000 ( 0.240941)

Here's the test code. The OBJECTS array contains 5 immediate objects and
5 non-immediate objects. Of course, with the "rescue" approach you get
a clone of the non-immediate object and in the "case" approach you
don't, but I'm assuming that you don't need the clone.

require 'benchmark'
include Benchmark

OBJECTS = [1, [], :sym, "astring", true, {}, false, 1..3, nil, /x/]
ITERATIONS = 10000

bm(2) do |x|
x.report("rescue") do
ITERATIONS.times do
OBJECTS.each do |obj|
begin
obj.clone
rescue
# nothing
end
end
end
end

x.report("case ") do
ITERATIONS.times do
OBJECTS.each do |obj|
case obj
when Fixnum, Symbol, TrueClass, FalseClass, NilClass
# nothing
else
# nothing
end
end
end
end

end
 
Reply With Quote
 
 
 
 
Gene Tani
Guest
Posts: n/a
 
      10-30-2005
you think #clone is slow, try (you could test for adding singleton
methods, that would be even slower!):

+ x.report("objSpace ") do
+ ITERATIONS.times do
+ OBJECTS.each do |obj|
+ begin
+ if ObjectSpace.include?(obj)
+ # nothin
+ end
+ rescue TypeError
+ # noting
+ end
+ end
+ end
+ end

Timothy Hunter wrote:
> Eric Mahurin wrote:
> > This is be best I could come up with for determining whether an
> > object is an immediate (Fixnum, Symbol, false, true, nil,
> > others?):
> >


 
Reply With Quote
 
Eric Mahurin
Guest
Posts: n/a
 
      10-30-2005
It is slow, but this is probably one of the better definitions
of what an immediate is - whether it exists in ObjectSpace or
not.

Here's another solution:

class Object; def immediate?;false;end;end
class Fixnum; def immediate?;true; end;end
class Symbol; def immediate?;true; end;end
class NilClass; def immediate?;true; end;end
class FalseClass;def immediate?;true; end;end
class TrueClass; def immediate?;true; end;end


--- Gene Tani <(E-Mail Removed)> wrote:

> you think #clone is slow, try (you could test for adding
> singleton
> methods, that would be even slower!):
>=20
> + x.report("objSpace ") do
> + ITERATIONS.times do
> + OBJECTS.each do |obj|
> + begin
> + if ObjectSpace.include?(obj)
> + # nothin
> + end
> + rescue TypeError
> + # noting
> + end
> + end
> + end
> + end
>=20
> Timothy Hunter wrote:
> > Eric Mahurin wrote:
> > > This is be best I could come up with for determining

> whether an
> > > object is an immediate (Fixnum, Symbol, false, true, nil,
> > > others?):
> > >

>=20
>=20
>=20



__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around=20
http://mail.yahoo.com=20


 
Reply With Quote
 
Gyoung-Yoon Noh
Guest
Posts: n/a
 
      10-31-2005
On 10/31/05, Eric Mahurin <(E-Mail Removed)> wrote:
> It is slow, but this is probably one of the better definitions
> of what an immediate is - whether it exists in ObjectSpace or
> not.
>
> Here's another solution:
>
> class Object; def immediate?;false;end;end
> class Fixnum; def immediate?;true; end;end
> class Symbol; def immediate?;true; end;end
> class NilClass; def immediate?;true; end;end
> class FalseClass;def immediate?;true; end;end
> class TrueClass; def immediate?;true; end;end
>


Here is one-line version:

[Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each { |c|
c.instance_eval { define_method(:immediate?) { true } } }

--
http://nohmad.sub-port.net


 
Reply With Quote
 
Gyoung-Yoon Noh
Guest
Posts: n/a
 
      10-31-2005
On 10/31/05, Gyoung-Yoon Noh <(E-Mail Removed)> wrote:
> On 10/31/05, Eric Mahurin <(E-Mail Removed)> wrote:
> > It is slow, but this is probably one of the better definitions
> > of what an immediate is - whether it exists in ObjectSpace or
> > not.
> >
> > Here's another solution:
> >
> > class Object; def immediate?;false;end;end
> > class Fixnum; def immediate?;true; end;end
> > class Symbol; def immediate?;true; end;end
> > class NilClass; def immediate?;true; end;end
> > class FalseClass;def immediate?;true; end;end
> > class TrueClass; def immediate?;true; end;end
> >

>
> Here is one-line version:
>
> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each { |c|
> c.instance_eval { define_method(:immediate?) { true } } }
>
> --
> http://nohmad.sub-port.net
>
>


Ouch, except Object:

>> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each {

|c| c.instance_eval { define_method(:immediate?) { (Object =3D=3D self) ?
false : true } } }
#=3D> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass]
>> Fixnum.immediate?

#=3D> true
>> Object.immediate?

#=3D> false

--
http://nohmad.sub-port.net


 
Reply With Quote
 
Ryan Leavengood
Guest
Posts: n/a
 
      10-31-2005
On 10/30/05, Gyoung-Yoon Noh <(E-Mail Removed)> wrote:
>
> Here is one-line version:
>
> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each { |c|
> c.instance_eval { define_method(:immediate?) { true } } }


Interesting, except the Object version needs to return false:

[Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each { |c|
c.instance_eval { define_method(:immediate?) { not c=3D=3DObject } } }

Ryan


 
Reply With Quote
 
Ryan Leavengood
Guest
Posts: n/a
 
      10-31-2005
On 10/30/05, Gyoung-Yoon Noh <(E-Mail Removed)> wrote:
>
> Ouch, except Object:
>
> >> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each {

> |c| c.instance_eval { define_method(:immediate?) { (Object =3D=3D self) ?
> false : true } } }
> #=3D> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass]
> >> Fixnum.immediate?

> #=3D> true
> >> Object.immediate?

> #=3D> false


But also:

irb(main):007:0> 'string'.immediate?
=3D> true
irb(main):008:0> [].immediate?
=3D> true
irb(main):009:0> Array.immediate?
=3D> true

Yours returns true for everything except Object. The other version I
posted is correct.

Ryan


 
Reply With Quote
 
Park Heesob
Guest
Posts: n/a
 
      10-31-2005
Hi,
----- Original Message -----
From: "Eric Mahurin" <(E-Mail Removed)>
To: "ruby-talk ML" <(E-Mail Removed)>
Sent: Monday, October 31, 2005 12:57 AM
Subject: Re: determining whether an object is an immediate?


> It is slow, but this is probably one of the better definitions
> of what an immediate is - whether it exists in ObjectSpace or
> not.
>
> Here's another solution:
>
> class Object; def immediate?;false;end;end
> class Fixnum; def immediate?;true; end;end
> class Symbol; def immediate?;true; end;end
> class NilClass; def immediate?;true; end;end
> class FalseClass;def immediate?;true; end;end
> class TrueClass; def immediate?;true; end;end
>

This would be better:

class Object
def immediate?
[Fixnum, Symbol, NilClass, FalseClass, TrueClass].include?(self.class)
end
end

Regards,

Park Heesob



 
Reply With Quote
 
Gyoung-Yoon Noh
Guest
Posts: n/a
 
      10-31-2005
On 10/31/05, Ryan Leavengood <(E-Mail Removed)> wrote:
> On 10/30/05, Gyoung-Yoon Noh <(E-Mail Removed)> wrote:
> >
> > Ouch, except Object:
> >
> > >> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass].each {

> > |c| c.instance_eval { define_method(:immediate?) { (Object =3D=3D self)=

?
> > false : true } } }
> > #=3D> [Object, Fixnum, Symbol, NilClass, FalseClass, TrueClass]
> > >> Fixnum.immediate?

> > #=3D> true
> > >> Object.immediate?

> > #=3D> false

>
> But also:
>
> irb(main):007:0> 'string'.immediate?
> =3D> true
> irb(main):008:0> [].immediate?
> =3D> true
> irb(main):009:0> Array.immediate?
> =3D> true
>
> Yours returns true for everything except Object. The other version I
> posted is correct.
>
> Ryan
>
>


Right. 'self' should not be there. Thanks for correcting.

--
http://nohmad.sub-port.net


 
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
Determining whether the current item is the last one from inside ItemDataBound Nathan Sokalski ASP .Net 5 05-21-2006 05:07 PM
boost facilities for determining whether or not a type supports particularsyntax? Howard Gardner C++ 17 04-25-2006 01:57 AM
Determining whether a macro has been defined BigMan C++ 8 02-17-2005 09:20 AM
Determining whether it's a file or a directory? Dfenestr8 Python 3 10-08-2004 11:04 AM
Determining whether String Array is empty news.amnet.net.au Java 5 09-08-2004 12:02 PM



Advertisments