Class behaviour

G

Geoff Barnes

Just curious, can I write a class that can check values as they are
assigned to objects?

foo = Klass.new
foo.legal_values = (4..10)

foo.value = 5 # OK
foo.value = 11 # ERROR - out of bounds, raise exception or something

Also, do I have to have an explicit method for the 'value', or can it
operate like a "builtin" class, like this :

foo = Klass.new
foo.legal_values = (4..10)

foo = 5 # OK
foo = 11 # ERROR - out of bounds, raise exception or something

In Perl, I would probably use tiescalar for something like this, where I
can intercept an assignment...

Also, it may not be numbers, but enums as well.

Thanks.
 
R

Robert Klemme

Geoff said:
Just curious, can I write a class that can check values as they are
assigned to objects?

foo = Klass.new
foo.legal_values = (4..10)

foo.value = 5 # OK
foo.value = 11 # ERROR - out of bounds, raise exception or something

You can define a special class method as a replacement for attr_accessor:

class Module
def checked_attr(name,&test)
define_method("#{name}=") do |val|
test[val] or raise ArgumentError
instance_variable_set "@#{name}", val
end

attr_reader name
end
end

class T
checked_attr :foo do |x|
x >= 0
end
end

irb(main):018:0* t=T.new
=> #<T:0x392088>
irb(main):019:0> t.foo = 10
=> 10
irb(main):020:0> t.foo = -10
ArgumentError: ArgumentError
from (irb):4:in `foo='
from (irb):20
from :0
irb(main):021:0>
Also, do I have to have an explicit method for the 'value', or can it
operate like a "builtin" class, like this :

foo = Klass.new
foo.legal_values = (4..10)

foo = 5 # OK
foo = 11 # ERROR - out of bounds, raise exception or something

You cannot interfere here so there is no way to check these assignments.
In Perl, I would probably use tiescalar for something like this, where I
can intercept an assignment...

Also, it may not be numbers, but enums as well.

I'm not sure I understand you here.

Kind regards

robert
 
P

Phrogz

Geoff said:
foo = Klass.new
foo.legal_values = (4..10)

foo.value = 5 # OK
foo.value = 11 # ERROR - out of bounds, raise exception or something

Do you want the legal values to be different per instance, or set for
the whole class?
Here are two options:

class Klass
class << self
attr_accessor :legal_values
end

attr_reader :value
def value=( new_value )
if value_range = self.class.legal_values
raise "Out of Range" unless value_range.include?( new_value )
end
@value = new_value
end
end

foo = Klass.new
bar = Klass.new
Klass.legal_values = 4..10

foo.value = 8
puts foo.value #=> 8

bar.value = 17 #=> RuntimeError: Out of Range


class Klass
attr_accessor :legal_values
attr_reader :value

def value=( new_value )
if @legal_values
raise "Out of Range" unless @legal_values.include?( new_value )
end
@value = new_value
end

def legal_values=( new_range )
raise "Value outside Range" if @value && !new_range.include?( value
)
@legal_values = new_range
end
end

foo = Klass.new
foo.legal_values = 4..10

foo.value = 8
puts foo.value #=> 8

bar = Klass.new
bar.value = 200
puts bar.value #=> 200

bar.legal_values = 1..10 #=> RuntimeError: Value outside Range


Also, do I have to have an explicit method for the 'value', or can it
operate like a "builtin" class, like this :

foo = Klass.new
foo.legal_values = (4..10)

foo = 5 # OK
foo = 11 # ERROR - out of bounds, raise exception or something

No, you can't do this - there is no assignment method (=) that you can
override to do something different. "foo = 5" will always change the
local variable 'foo' to point to a Fixnum of 5.
 

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
474,211
Messages
2,571,092
Members
47,693
Latest member
david4523

Latest Threads

Top