From: Ralph Shnelvar on 11 Aug 2010 19:44 [Note: parts of this message were removed to make it a legal post.] I'm still not "getting it." Consider - - - class DirectoryString < String def initialize(arg) super arg end def convert_forward_slash_to_back_slash self.gsub(/\//, "\\") end def convert_forward_slash_to_back_slash! self.gsub!(/\//, "\\") end def remove_trailing_backslash_if_any self.chomp("\\") end def remove_trailing_backslash_if_any! self.chomp!("\\") end def append_trailing_backslash_if_needed remove_trailing_backslash_if_any + "\\" end def append_trailing_backslash_if_needed! # ??????? What do I do here ??????? ... The following is illegal self = "Help!" end end - - - The only thing I can think of is converting from the standard IS_A (Inheritance) to a HAS_A (create a member variable). If I do the latter, above, I will have to create a ton of simple "forwarding" messages. What's the Ruby way of doing this sort of thing?
From: jeremy Ruten on 11 Aug 2010 19:52 You can use the #replace method to do that sort of thing: self.replace "Help! Oh, well, I'm saved!" This method is defined in classes String, Hash, Array, and Set, and works the same way in each one. jeremy On Wed, Aug 11, 2010 at 5:44 PM, Ralph Shnelvar <ralphs(a)dos32.com> wrote: > I'm still not "getting it." > > Consider > > - - - > class DirectoryString < String > def initialize(arg) > super arg > end > > def convert_forward_slash_to_back_slash > self.gsub(/\//, "\\") > end > > def convert_forward_slash_to_back_slash! > self.gsub!(/\//, "\\") > end > > def remove_trailing_backslash_if_any > self.chomp("\\") > end > > def remove_trailing_backslash_if_any! > self.chomp!("\\") > end > > def append_trailing_backslash_if_needed > remove_trailing_backslash_if_any + "\\" > end > > def append_trailing_backslash_if_needed! > # ??????? What do I do here ??????? ... The following is illegal > self = "Help!" > end > end > > - - - > > > The only thing I can think of is converting from the standard IS_A (Inheritance) to a HAS_A (create a member variable). > > If I do the latter, above, I will have to create a ton of simple "forwarding" messages. > > What's the Ruby way of doing this sort of thing?
From: Ralph Shnelvar on 11 Aug 2010 20:12 [Note: parts of this message were removed to make it a legal post.] Wednesday, August 11, 2010, 5:52:57 PM, you wrote: jR> You can use the #replace method to do that sort of thing: jR> self.replace "Help! Oh, well, I'm saved!" jR> This method is defined in classes String, Hash, Array, and Set, and jR> works the same way in each one. jR> jeremy jeremy, That's great and solves my particular problem. But what if the class being derived from does not have a replace method? Ralph
From: Ralph Shnelvar on 11 Aug 2010 20:38 [Note: parts of this message were removed to make it a legal post.] David, >> That's great and solves my particular problem. >> But what if the class being derived from does not have a replace method? DAB> If the object has state that can be modified, then there will be (by DAB> definition) ways to modify that state. If it doesn't, then there won't DAB> be, and the class in question is probably a bad starting point if you DAB> want to create objects with state that can be modified. DAB> That's one of the advantages of using proxy objects and delegators: you DAB> gain an extra axis along which you can make decisions about things like DAB> object state. Even though you can't change (say) a Fixnum, you can DAB> create objects with integer attributes that *can* be changed. Could you expand on this with an example, please?
From: David A. Black on 11 Aug 2010 20:29 On Thu, 12 Aug 2010, Ralph Shnelvar wrote: > Wednesday, August 11, 2010, 5:52:57 PM, you wrote: > > jR> You can use the #replace method to do that sort of thing: > > jR> self.replace "Help! Oh, well, I'm saved!" > > jR> This method is defined in classes String, Hash, Array, and Set, and > jR> works the same way in each one. > > jR> jeremy > > > jeremy, > > That's great and solves my particular problem. > > But what if the class being derived from does not have a replace method? If the object has state that can be modified, then there will be (by definition) ways to modify that state. If it doesn't, then there won't be, and the class in question is probably a bad starting point if you want to create objects with state that can be modified. That's one of the advantages of using proxy objects and delegators: you gain an extra axis along which you can make decisions about things like object state. Even though you can't change (say) a Fixnum, you can create objects with integer attributes that *can* be changed. David -- David A. Black, Senior Developer, Cyrus Innovation Inc. The Ruby training with Black/Brown/McAnally Compleat Philadelphia, PA, October 1-2, 2010 Rubyist http://www.compleatrubyist.com
|
Next
|
Last
Pages: 1 2 Prev: syntax error, unexpected '}', expecting kEND Next: self in irb versus script |