L
Leslie Viljoen
Sorry to beat a dead horse, but to confirm: the only way to mix a
module into an already
existing class it to reopen it with something (ugly) like this:
class String; include Crc; end
(1) Is that right?
And then to mix a module into an instantiated object, you can use
extend, and that
modifies the one object. (2) Philosophically, is there any reason why
extend can't be used
in the same way on classes? (ie. String.extend(Crc))
One other question (3), below is an example of my SelfCrc module. This module
works well when included into String, but extend doesn't work because that
only modifies the one object and .crc16_ok? below tries to call .get_crc16
on a new string that it creates, which fails.
I can modify it to work like so:
def crc16_ok?
first_part = self[0..-3]
first_part.extend(SelfCrc)
first_part.get_crc16 == get_terminating_crc16
end
...is that the right way to make this module work properly? Or are
some modules just not suitable for extend?
module SelfCrc
# Returns the CCITT CRC16 for the current object
#
def get_crc16
@@crctab ||= CrcTab.new
@@crctab.get(self)
end
# Converts the last two characters of this string to
# a numeric CRC and returns it
#
def get_terminating_crc16
(self[-2, 1][0] << 8) + (self[-1, 1][0])
end
# Calculates a CRC for the string, converts it into two
# characters and adds them to the end of the string
#
def add_terminating_crc16!
replace(self + get_crc16.to_s16)
end
# Removes the last two characters from the string
#
def remove_terminating_crc16!
replace(self[0..-3])
end
# Extracts two characters from the end of the string, converts
# them to a numeric CRC and compares that CRC to the CRC of
# the rest of the string. Returns true if the CRC is correct.
#
def crc16_ok?
self[0..-3].get_crc16 == get_terminating_crc16
end
end
Thanks!
Leslie
module into an already
existing class it to reopen it with something (ugly) like this:
class String; include Crc; end
(1) Is that right?
And then to mix a module into an instantiated object, you can use
extend, and that
modifies the one object. (2) Philosophically, is there any reason why
extend can't be used
in the same way on classes? (ie. String.extend(Crc))
One other question (3), below is an example of my SelfCrc module. This module
works well when included into String, but extend doesn't work because that
only modifies the one object and .crc16_ok? below tries to call .get_crc16
on a new string that it creates, which fails.
I can modify it to work like so:
def crc16_ok?
first_part = self[0..-3]
first_part.extend(SelfCrc)
first_part.get_crc16 == get_terminating_crc16
end
...is that the right way to make this module work properly? Or are
some modules just not suitable for extend?
module SelfCrc
# Returns the CCITT CRC16 for the current object
#
def get_crc16
@@crctab ||= CrcTab.new
@@crctab.get(self)
end
# Converts the last two characters of this string to
# a numeric CRC and returns it
#
def get_terminating_crc16
(self[-2, 1][0] << 8) + (self[-1, 1][0])
end
# Calculates a CRC for the string, converts it into two
# characters and adds them to the end of the string
#
def add_terminating_crc16!
replace(self + get_crc16.to_s16)
end
# Removes the last two characters from the string
#
def remove_terminating_crc16!
replace(self[0..-3])
end
# Extracts two characters from the end of the string, converts
# them to a numeric CRC and compares that CRC to the CRC of
# the rest of the string. Returns true if the CRC is correct.
#
def crc16_ok?
self[0..-3].get_crc16 == get_terminating_crc16
end
end
Thanks!
Leslie