What is a good naming convention for clearing an instance variable via a method. The actual value is stored in an array and the methods filter the array to find the value.
Here are methods I already have:
class Foo
def initialize
# Array of objects
@data = []
end
# Get the variable or default value
def variable
@data.select { |o| o.attribute == 'Some Value' }.first || 'Default Value'
end
# Does the variable exist
def variable?
[email protected] { |o| o.attribute == 'Some Value' }.first
end
# Does this make sense?
def clear_variable!
# Delete the variable
end
end
Or should it be something like delete_variable!?
If you're creating something like that, it's best to emulate the conventional method names used within the most similar structure in Ruby. In this case, it's Hash:
class Foo
def initialize
@data = [ ]
@default = 'Default Value'
end
def [](k)
found = @data.find { |o| o.attribute == k }
found ? found.value : @default
end
def has_key(k)?
[email protected] { |o| o.attribute == k }
end
# Does this make sense?
def delete(k)
@data.reject! { |o| o.attribute == k }
end
end
Generally a method with ! in it either raises exceptions if something goes wrong, it makes a permanent modification to the state of something (e.g. in-place modification methods), or both. It's not taken to mean "reset" or "clear".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With