N
Nate
Hi everyone. We're noticing some strange behavior when including
modules in classes. It seems that even if a conditional block is
forced to evaluate as false (and presumably not execute), assignments
in the conditional block are executed and set to nil. Here is an
example:
==========================
module Conundrum
def riddle
@riddle ||= 'mystery'
end
def riddle=(new_val)
puts "Called riddle="
@riddle = new_val
end
end
class Test
include Conundrum
def test
puts "Before: #{riddle}"
if true == false
riddle = 'solved!'
end
puts "After: #{riddle}"
puts "After class: #{riddle.class}"
end
end
Test.new.test
==========================
Note that this prints out:
Before: mystery
After:
After class: NilClass
So it would seem that riddle= is not getting called, but yet the
riddle function now returns nil. Interestingly, this fixes the
problem:
==========================
module Conundrum
def riddle
@riddle ||= 'mystery'
end
def riddle=(new_val)
puts "Called riddle="
@riddle = new_val
end
end
class Test
include Conundrum
def test
puts "Before: #{riddle}"
if true == false
@riddle = 'solved!'
end
puts "After: #{riddle}"
puts "After class: #{riddle.class}"
end
end
Test.new.test
==========================
Outputs:
Before: mystery
After: mystery
After class: String
Any insights are much appreciated!
-n8
modules in classes. It seems that even if a conditional block is
forced to evaluate as false (and presumably not execute), assignments
in the conditional block are executed and set to nil. Here is an
example:
==========================
module Conundrum
def riddle
@riddle ||= 'mystery'
end
def riddle=(new_val)
puts "Called riddle="
@riddle = new_val
end
end
class Test
include Conundrum
def test
puts "Before: #{riddle}"
if true == false
riddle = 'solved!'
end
puts "After: #{riddle}"
puts "After class: #{riddle.class}"
end
end
Test.new.test
==========================
Note that this prints out:
Before: mystery
After:
After class: NilClass
So it would seem that riddle= is not getting called, but yet the
riddle function now returns nil. Interestingly, this fixes the
problem:
==========================
module Conundrum
def riddle
@riddle ||= 'mystery'
end
def riddle=(new_val)
puts "Called riddle="
@riddle = new_val
end
end
class Test
include Conundrum
def test
puts "Before: #{riddle}"
if true == false
@riddle = 'solved!'
end
puts "After: #{riddle}"
puts "After class: #{riddle.class}"
end
end
Test.new.test
==========================
Outputs:
Before: mystery
After: mystery
After class: String
Any insights are much appreciated!
-n8