Can't change the value of self - II

R

Ralph Shnelvar

[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?
 
J

jeremy Ruten

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

I'm still not "getting it."

Consider

- - -
class DirectoryString < String
=A0def initialize(arg)
=A0 =A0super arg
=A0end

=A0def convert_forward_slash_to_back_slash
=A0 =A0self.gsub(/\//, "\\")
=A0end

=A0def convert_forward_slash_to_back_slash!
=A0 =A0self.gsub!(/\//, "\\")
=A0end

=A0def remove_trailing_backslash_if_any
=A0 =A0self.chomp("\\")
=A0end

=A0def remove_trailing_backslash_if_any!
=A0 =A0self.chomp!("\\")
=A0end

=A0def append_trailing_backslash_if_needed
=A0 =A0remove_trailing_backslash_if_any + "\\"
=A0end

=A0def append_trailing_backslash_if_needed!
=A0 =A0# ??????? What do I do here ??????? ... The following is illegal
=A0 =A0self =3D "Help!"
=A0end
end

- - -


The only thing I can think of is converting from the standard IS_A (Inher=
itance) to a HAS_A (create a member variable).
 
R

Ralph Shnelvar

[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
 
D

David A. Black

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
 
R

Ralph Shnelvar

[Note: parts of this message were removed to make it a legal post.]

David,

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?
 
J

Jesús Gabriel y Galán

David,


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?

irb(main):001:0> require 'delegate'
=> true
irb(main):002:0> class MutableFixnum < Delegator
irb(main):003:1> def initialize i
irb(main):004:2> super
irb(main):005:2> @value = i
irb(main):006:2> end
irb(main):007:1> def __getobj__
irb(main):008:2> @value
irb(main):009:2> end
irb(main):014:1> def __setobj__ value
irb(main):015:2> @value = value
irb(main):016:2> end
irb(main):017:1> def add! x
irb(main):018:2> __setobj__(__getobj__ + x)
irb(main):019:2> end
irb(main):020:1> end
=> nil
irb(main):021:0> a = MutableFixnum.new 1
=> 1
irb(main):022:0> a + 3
=> 4
irb(main):023:0> a.add! 3
=> 4
irb(main):024:0> a
=> 4

Jesus.
 
B

Brian Candler

Ralph said:
Could you expand on this with an example, please?

[Just to show that delegation is nothing magic, and you don't need to
use the Delegator class]

class MyCounter
def initialize(n=0)
@n = n
end
def inc!
@n += 1
end
def to_int
@n
end
def to_s
@n.to_s
end
end

c = MyCounter.new
c.inc!
c.inc!
puts "c is #{c}"

Some more work is required to make c duck-type like an Integer though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top