Skip to content

Instantly share code, notes, and snippets.

@tommeier
Created October 5, 2010 01:44
Show Gist options
  • Save tommeier/610826 to your computer and use it in GitHub Desktop.
Save tommeier/610826 to your computer and use it in GitHub Desktop.
class Something
CONSTANT_VALUES = ["Hooray - I have substance!", "Something Else"]
def self.do_something
puts "Constant value = #{Something::CONSTANT_VALUES.inspect}"
puts "Method Value = #{self.apply_something(Something::CONSTANT_VALUES)}"
end
def self.apply_something(values)
value_changed = [*values]
value_changed.slice!(0,30).each do |value|
#Do any processing
end
values
end
end
--------------
>> Something.do_something
Constant value = ["Hooray - I have substance!", "Something Else"]
Method Value =
=> nil
>> Something.do_something
Constant value = []
Method Value =
=> nil
>> Something.do_something
Constant value = []
Method Value =
=> nil
--------------
Ruby changes constants :( - in this instance it is the .slice! doing the dirty deed, to correct this we still need a 'dup' even on passed constants:
class Something
CONSTANT_VALUES = ["Hooray - I have substance!", "Something Else"]
def self.do_something
puts "Constant value = #{Something::CONSTANT_VALUES.inspect}"
puts "Method Value = #{self.apply_something(Something::CONSTANT_VALUES)}"
end
def self.apply_something(values)
value_changed = [*values.dup]
value_changed.slice!(0,30).each do |value|
#Do any processing
end
values
end
end
---------------
>> Something.do_something
Constant value = ["Hooray - I have substance!", "Something Else"]
Method Value = Hooray - I have substance!Something Else
=> nil
>> Something.do_something
Constant value = ["Hooray - I have substance!", "Something Else"]
Method Value = Hooray - I have substance!Something Else
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment