# Converting from one class to another in Ruby

**URL:** <https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842>\
**Category:** Ruby\
**Created:** [September 12, 2010, 3:34am UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842 "2010-09-12T03:34:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 12, 2010, 3:34am UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/1 "2010-09-12T03:34:40Z")

</div>

Dear list members,

I have an application with a class Binary\_matrix \< Array (which is just  
a two dimensional array filled with zeroes and ones). I also have other  
classes which have to\_Binary\_matrix methods to convert objects of their  
class to Binary\_matrix objects.

The Ruby code I came up with to do the conversions is as follows.

class A  
def to\_Binary\_matrix(from\_initialize\_method = false)  
…some logic to convert class A object to binary\_matrix\_format  
if from\_initialize\_method  
return an\_A\_type\_object\_in\_binary\_matrix\_format  
else  
return  
Binary\_matrix.new(an\_A\_type\_object\_in\_binary\_matrix\_format)  
end  
end  
end

class B  
def to\_Binary\_matrix(from\_initialize\_method = false)  
…some logic to convert class B object to binary\_matrix\_format  
if from\_initialize\_method  
return a\_B\_type\_object\_in\_binary\_matrix\_format  
else  
return  
Binary\_matrix.new(a\_B\_type\_object\_in\_binary\_matrix\_format)  
end  
end  
end

class Binary\_matrix \< Array  
def initialize arg  
self.replace arg.to\_Binary\_matrix(true)  
end  
end

This allows me to do:

a = A.new  
an\_A\_as\_a\_Binary\_matrix\_object = a.to\_Binary\_matrix  
b\_as\_a\_Binary\_matrix\_object = Binary\_matrix.new(b)

It works but it seems a little clunky to me. Is their a more Rubyish or  
elegent way of doing this. In particular is it possible to get rid of  
the flag I use (from\_initialize\_method) to keep me from getting into an  
infinite loop as I go between the .to\_Binary\_matrix methods and the  
initialize method in Binary\_matrix

Thanks in advance,  
Harry

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 13, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/2 "2010-09-13T16:22:10Z")

</div>

On Sun, Sep 12, 2010 at 3:34 AM, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Dear list members,
> 
> I have an application with a class Binary\_matrix \< Array (which is just

The first thing I’d do differently is to cut off this inheritance.  
Inheriting from core classes is rarely a good idea. In this case you  
_use_ an array for your storage of a binary matrix which is an  
implementation detail that should be hidden inside the class. Also I  
am sure you do not want all public Array methods to be available for  
users of Binary\_matrix. So delegation is a much better option here.

> ```
> return an_A_type_object_in_binary_matrix_format
> 
> ```
> 
> else  
> return  
> Binary\_matrix.new(an\_A\_type\_object\_in\_binary\_matrix\_format)  
> end  
> end  
> end

I find it hard to come up with good suggestions since a lot is missing  
in this example (for example how you call it from #initialize). Why  
don’t you have a method  
some\_logic\_to\_convert\_class\_A\_object\_to binary\_matrix\_format which is  
then called from #initialize and from to\_Binary\_matrix? That seems  
the most natural solution and then you do not need the flag in the  
first place.

> end  
> an\_A\_as\_a\_Binary\_matrix\_object = a.to\_Binary\_matrix  
> b\_as\_a\_Binary\_matrix\_object = Binary\_matrix.new(b)
> 
> It works but it seems a little clunky to me. Is their a more Rubyish or  
> elegent way of doing this. In particular is it possible to get rid of  
> the flag I use (from\_initialize\_method) to keep me from getting into an  
> infinite loop as I go between the .to\_Binary\_matrix methods and the  
> initialize method in Binary\_matrix

It looks absolutely clunky.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 4:35am UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/3 "2010-09-14T04:35:07Z")

</div>

Dear Robert,

Many thanks for your reply.  
I have simplified somewhat. Its an OCR application where Binary\_matrix  
is a black and white pixel representation of a page of text(or line of  
text, or letter or part of a letter etc.). There are many different  
objects which are to be converted into Binary\_matrix such as the above  
portions of an existing Binary\_matrix object, or Rmagick ImageList  
objects, or graphic objects processed by other packages than RMagick.  
This list will definitely grow in the future so for that reason I wanted  
the conversion to Binary\_matrix logic to reside in the class being  
converted to Binary\_matrix not in the Binary\_matrix class.

I.e. I didn’t want a list of statements in Binary\_matrix initialize  
method like:  
if arg.class == ImageList then ImageList\_conversion end  
if arg.class == JPEG then jpeg\_conversion end  
etc. etc.  
Rather I wanted my Binary\_matrix object to check if the object to be  
converted to a Binary\_matrix had a to\_Binary\_matrix method and then to  
call it.

I also require all the Array methods for Binary\_matrix because of the  
complex traversing and manipulation of the pixel data. To give a  
trivial example: does a black pixel in line n touch a black pixel in  
line n+1? What are the array ranges of a contiguous group of black  
pixels etc. Change a contiguous range of pixels to white (i.e. clear up  
noise) etc.

Again because of complexity of the application I wanted to make the code  
as humanly readable as possible. So I want to be able to say  
page\_in\_pixels = page\_image.to\_Binary\_matrix  
as well as  
page\_in\_pixels = Binary\_matrix(page\_image)  
(I know that may sound trivial but when you go back to look at your code  
a few months later, everything that makes it more easily readable  
helps.)  
So for those reasons the solution I came up with is as follows:

(I’m not saying its necessarily a good solution and I really wish I  
could make it less clunky. if there was some way to tell whether method  
to\_Binary\_matrix was being called within the context of Binary\_matrix  
initialize method or outside it then the flag -from\_initialize\_method-  
wouldn’t be necessary)

class A  
def to\_Binary\_matrix(from\_initialize\_method = false)  
if from\_initialize\_method  
return self  
else  
return Binary\_matrix.new(self)  
end  
end  
…  
end

class Binary\_matrix \< Array  
def initialize(arg)  
called\_from\_initialize =true  
if arg.respond\_to? :to\_Binary\_matrix  
arg = arg.to\_Binary\_matrix (called\_from\_initialize)  
else  
…do some error reporting  
end  
validate arg #Is the Binary\_matrix object correctly formed?  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 5:50am UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/4 "2010-09-14T05:50:07Z")

</div>

Correction to post (finger trouble)  
Harry S. wrote:

> class A  
> def to\_Binary\_matrix(from\_initialize\_method = false)  
> if from\_initialize\_method  
> …logic to convert to Binary\_matrix format  
> and store result in two\_dimensional\_binary\_array…  
> return two\_dimensional\_binary\_array  
> else  
> return Binary\_matrix.new(self)  
> end  
> end  
> …  
> end
> 
> class Binary\_matrix \< Array  
> def initialize(arg)  
> called\_from\_initialize =true  
> if arg.respond\_to? :to\_Binary\_matrix  
> arg = arg.to\_Binary\_matrix (called\_from\_initialize)  
> else  
> …do some error reporting  
> end  
> validate arg #Is the Binary\_matrix object correctly formed?  
> self.replace arg  
> end  
> …  
> end

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 11:27am UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/5 "2010-09-14T11:27:45Z")

</div>

On Tue, Sep 14, 2010 at 4:35 AM, Harry S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Many thanks for your reply.

You’re welcome.

> I have simplified somewhat. Its an OCR application where Binary\_matrix  
> is a black and white pixel representation of a page of text(or line of  
> text, or letter or part of a letter etc.). There are many different  
> objects which are to be converted into Binary\_matrix such as the above  
> portions of an existing Binary\_matrix object, or Rmagick ImageList  
> objects, or graphic objects processed by other packages than RMagick.  
> This list will definitely grow in the future so for that reason I wanted  
> the conversion to Binary\_matrix logic to reside in the class being  
> converted to Binary\_matrix not in the Binary\_matrix class.

That’s perfectly OK. Just the way you coded your logic is weird IMHO.  
Just do this for your classes:

class A  
def initialize( what goes here? )  
# what do you want to do here?  
end

def to\_binary\_matrix  
bm = BinaryMatrix.new  
# fill bm  
bm  
end  
end

> I.e. I didn’t want a list of statements in Binary\_matrix initialize  
> method like:  
> if arg.class == ImageList then ImageList\_conversion end  
> if arg.class == JPEG then jpeg\_conversion end  
> etc. etc.

I would not want it that way either nor did I suggest this.

> Rather I wanted my Binary\_matrix object to check if the object to be  
> converted to a Binary\_matrix had a to\_Binary\_matrix method and then to  
> call it.

Just call it (see below).

> I also require all the Array methods for Binary\_matrix because of the  
> complex traversing and manipulation of the pixel data. To give a  
> trivial example: does a black pixel in line n touch a black pixel in  
> line n+1? What are the array ranges of a contiguous group of black  
> pixels etc. Change a contiguous range of pixels to white (i.e. clear up  
> noise) etc.

These are the exact operations that I would place in a class  
BinaryMatrix (btw. note the naming conventions for Ruby constants).  
An Array is just not a matrix. By exposing all Array methods client  
code (i.e. classes that use BinaryMatrix) needs to know about the  
internal representation of a BinaryMatrix which is about the worst  
thing you can do in an OO application from a design perspective.

> Again because of complexity of the application I wanted to make the code  
> as humanly readable as possible. So I want to be able to say  
> page\_in\_pixels = page\_image.to\_Binary\_matrix  
> as well as  
> page\_in\_pixels = Binary\_matrix(page\_image)

For that to work you only need

def BinaryMatrix(x)  
x.to\_binary\_matrix  
end

and implement #to\_binary\_matrix in all your classes properly.

> (I’m not saying its necessarily a good solution and I really wish I  
> could make it less clunky. if there was some way to tell whether method  
> to\_Binary\_matrix was being called within the context of Binary\_matrix  
> initialize method or outside it then the flag -from\_initialize\_method-  
> wouldn’t be necessary)

Why and what do you want to convert to BM in #initialize? This hasn’t  
become clear yet.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 5:57pm UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/6 "2010-09-14T17:57:57Z")

</div>

Robert K. wrote:

> Just do this for your classes:
> 
> class A  
> def initialize( what goes here? )  
> # what do you want to do here?  
> end
> 
> def to\_binary\_matrix  
> bm = BinaryMatrix.new  
> # fill bm  
> bm  
> end  
> end
> 
> > Rather I wanted my Binary\_matrix object to check if the object to be  
> > converted to a Binary\_matrix had a to\_Binary\_matrix method and then to  
> > call it.
> 
> Just call it (see below).

> Why and what do you want to convert to BM in #initialize? This hasn’t  
> become clear yet.

I wrote

> > I want to be able to say  
> > page\_in\_pixels = page\_image.to\_Binary\_matrix  
> > as well as  
> > page\_in\_pixels = Binary\_matrix(page\_image)

I meant to write: (more finger trouble 🙂 )  
I want to be able to do both:  
page\_in\_pixels = page\_image.to\_Binary\_matrix  
page\_in\_pixels = Binary\_matrix.new(page\_image)  
(i.e. Binary\_matrix.new doesn’t create an empty Binary\_matrix object but  
takes as a parameter an object which is converted to a Binary\_matrix )

so for that reason my solution had as follows:

1. obj\_a.to\_Binary\_matrix calls Binary\_matrix.new to create a new  
Binary\_matrix object.  
I.e.In this case #to\_Binary\_matrix is being called outside the context  
of Binary\_matrix#initialize

but

2)Binary\_matrix#initialize contains an arg = arg.to\_Binary\_matrix  
statement to access the conversion logic.  
I.e. #to\_Binary\_matrix is being called within the context of  
Binary\_matrix#initialize

so my solution (retyped at the end of this post had a flag to keep from  
getting in an infinite loop and to do the following:

(I’m not sure if I’m missing something really obvious but I still don’t  
see that I don’t need a flag or someway to tell if #to\_Binary\_matrix is  
being called within Binary\_matrix#initialize or not)

case 1:  
p = page\_image.to\_Binary\_matrix  
calls page\_image.to\_Binary\_matrix  
–\> calls Binary\_matrix.new(page\_image) since flag\_not\_set  
Binary\_matrix.initialize(arg)  
arg = arg.to\_Binary\_matrix(flag\_set)  
–\>calls arg.to\_Binary\_matrix(flag\_set)  
convert page\_image to binary format since flag\_set  
Binary\_matrix \<-- return binary matrix format since flag\_set  
\<-----Binary\_matrix.new returns Binary\_matrix obj

case 2:  
p = Binary\_matrix.new(page\_image)  
calls Binary\_matrix.new(page\_image)  
–\> Binary\_matrix.initialize(arg)  
arg = arg.to\_Binary\_matrix(flag\_set)  
–\>calls arg.to\_Binary\_matrix(flag\_set)  
–\>calls arg.to\_Binary\_matrix(flag\_set)  
convert page\_image to Binary\_matrix format since  
flag\_set  
Binary\_matrix \<-- return binary matrix format  
\<-----Binary\_matrix.new returns Binary\_matrix obj

class Binary\_matrix \< Array  
def initialize(arg)  
called\_from\_initialize =true  
if arg.respond\_to? :to\_Binary\_matrix  
arg = arg.to\_Binary\_matrix (called\_from\_initialize)  
else  
…do some error reporting  
end  
validate arg #Is the Binary\_matrix object correctly formed?  
self.replace arg  
end  
…  
end

class A  
def to\_Binary\_matrix(from\_initialize\_method = false)  
if from\_initialize\_method  
…logic to convert to Binary\_matrix format  
and store result in two\_dimensional\_binary\_array…  
return two\_dimensional\_binary\_array  
else  
return Binary\_matrix.new(self)  
end  
end  
…  
end

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/7 "2010-09-14T18:56:48Z")

</div>

On 14.09.2010 17:58, Harry S. wrote:

> > ```
> > bm = BinaryMatrix.new
> > 
> > ```
> > 
> > Just call it (see below).
> 
> I meant to write: (more finger trouble 🙂 )

Hopefully the trouble does not extend further up… 🙂

> I want to be able to do both:  
> page\_in\_pixels = page\_image.to\_Binary\_matrix  
> page\_in\_pixels = Binary\_matrix.new(page\_image)  
> (i.e. Binary\_matrix.new doesn’t create an empty Binary\_matrix object but  
> takes as a parameter an object which is converted to a Binary\_matrix )

This seems quite backwards to me. What do you gain by this other than  
making the code more complicated than necessary? You say it feels  
awkward and still seem to insist on this approach.

> I.e. #to\_Binary\_matrix is being called within the context of  
> Binary\_matrix#initialize

You can have it easier

class A  
def to\_binary\_matrix  
bm = BinaryMatrix.new  
fill bm  
bm  
end

def fill bm  
# do whatever it takes to make state of self  
# appear in bm  
end  
end

class BinaryMatrix  
def initialize(x = nil)  
x.fill self rescue nil  
end  
end

IIRC this is similar although not identical to what I have suggested in  
my first posting. But frankly, I find this only slightly less awkward.  
In my experience providing too many ways to do things is rather  
causing trouble than improving things. Plus, #to\_x is a common idiom in  
Ruby to provide conversions of something into something else - in fact  
it is quite ubiquitous).

> so my solution (retyped at the end of this post had a flag to keep from  
> getting in an infinite loop and to do the following:
> 
> (I’m not sure if I’m missing something really obvious but I still don’t  
> see that I don’t need a flag or someway to tell if #to\_Binary\_matrix is  
> being called within Binary\_matrix#initialize or not)

See above.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![harryspier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@harryspier](https://www.ruby-forum.com/u/harryspier)\
**Post date:** [September 14, 2010, 7:25pm UTC](https://www.ruby-forum.com/t/converting-from-one-class-to-another-in-ruby/194842/8 "2010-09-14T19:25:29Z")

</div>

Thanks Robert,

I’ll try that  
Cheers
