From: Harry Spier Date: 2010-09-15T00:58:00+09:00 Subject: Re: Converting from one class to another in Ruby Robert Klemme 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 -- Posted via http://www.ruby-forum.com/.