The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Structural equivalence of Ruby objects

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eigen Class

Posts: 358
Nickname: eigenclass
Registered: Oct, 2005

Eigenclass is a hardcore Ruby blog.
Structural equivalence of Ruby objects Posted: Nov 28, 2005 10:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Eigen Class.
Original Post: Structural equivalence of Ruby objects
Feed Title: Eigenclass
Feed URL: http://feeds.feedburner.com/eigenclass
Feed Description: Ruby stuff --- trying to stay away from triviality.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Eigen Class
Latest Posts From Eigenclass

Advertisement

I have generalized the recursion-safe routine used to compare two arrays, so that it now determines whether two objects are "structurally equivalent". It is fairly powerful, being able to track recursion across instance variables, arrays...

What's this good for?

The obvious application would be checking whether some objects returned by by the code under test have the desired structure.

Examples

Here's a taste of what it can do... I'll be using the following class to illustrate the semantics of #struct_eql?.

 class A
   attr_accessor :a, :b
   
   def initialize(a,b)
     @a, @b = a, b
   end
 end

Let's perform some simple tests:

 a = A.new(1, 2)
 a.struct_eql?(A.new(2,3))                       # => true

A.new(2,3) is considered "structurally equivalent" to A.new(1,2) because both objects have two instance variables named @a and @b, and they both refer to Fixnum objects. That's simple, and explains why the following don't match:

 a.struct_eql?(A.new("",1))                      # => false

Object identity

We've seen the basics of the definition of "structural equivalence" proposed here, but there's more. In the above examples, we had another implicit condition:

 a.struct_eql?(A.new(1,1))                       # => false

The "structural equivalence" conditions expressed by A.new(1,2).struct_eql?(b) could be rephrased as:

  • b must be an object of class A (or descendants)
  • b must have two instance variables named @a and @b
  • @a and @b must refer to two Fixnum objects
  • @a must be different from @b

The last condition can be reversed to express things like "both instance variables must be equal":

 a = A.new(1,1)
 a.struct_eql?(A.new(2,2))                       # => true
 a.struct_eql?(A.new(2,1))                       # => false

Recursive structures

We can test recursive data for structural equivalence:

 a.b = a
 a.struct_eql?(A.new(2,A.new(2,nil)))            # => false
 a.struct_eql?(A.new(1,1))                       # => false
 b = A.new(5, nil)
 b.b = b
 a.struct_eql?(b)                                # => true
  
 b = A.new(2,2)
 b.b = b
 a.struct_eql?(b)                                # => true

It is also possible to perform tests which involve both object identity and recursion at a time:

 a.b = A.new(A.new(2,A.new(1,a)), 2)             
 b = A.new(5,nil)
 b.b = A.new(A.new(5,A.new(5,b)), 5)
 a.struct_eql?(b)                                # => false

a and b aren't structurally equal in the above example because b.b.a.a is equal to b.b.a.b.a, which was prohibited by the original expression:

 a.b = A.new(A.new(2,A.new(1,a)), 2)             
                   ^       ^
                different values

We get the expected result if those values are changed suitably:

 b.b = A.new(A.new(1,A.new(5,b)), 1)
 a.struct_eql?(b)                                # => true

Read more...

Read: Structural equivalence of Ruby objects

Topic: My mind and continuations, callcc seen graphically Previous Topic   Next Topic Topic: Xampl For Ruby is Released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use