You have defined @transactions twice, initially when self equals TestRun, making it a class instance variable, then within the print method, when self is the instance TestRun.new, making that one an instance variable. Those two variables are as different as @night and @day.
Here are three ways to make your code work (the first two being for educational purposes only).
Make print a class method
class TestRun
puts "self at class level = #{self}"
@transactions = [
{:repayment_number => 1, :repayment_amount => 224.34},
{:repayment_number => 2, :repayment_amount => 241.50}
]
def self.print
puts "self within self.print = #{self}"
@transactions.each do |t|
puts "#{t[:repayment_number]} - #{t[:repayment_amount]}"
end
end
end
#=> self at class level = TestRun
TestRun.print
#=> self within self.print = TestRun
# 1 - 224.34
# 2 - 241.5
Access the class instance variable from within the method print
class TestRun
@transactions = [
{:repayment_number => 1, :repayment_amount => 224.34},
{:repayment_number => 2, :repayment_amount => 241.50}
]
def print
puts "self within print = #{self}"
self.class.instance_variable_get(:@transactions).each do |t|
puts "#{t[:repayment_number]} - #{t[:repayment_amount]}"
end
end
end
TestRun.new.print
#=> self within print = #<TestRun:0x007fcccb13f390>
# 1 - 224.34
# 2 - 241.5
Define @transactions within the initialize method, making it an instance variable
This is what @31piy has done, and most likely what is intended by the OP.
class TestRun
def initialize
puts "self within initialize = #{self}"
@transactions = [
{:repayment_number => 1, :repayment_amount => 224.34},
{:repayment_number => 2, :repayment_amount => 241.50}
]
end
def print
puts "self within print = #{self}"
@transactions.each do |t|
puts "#{t[:repayment_number]} - #{t[:repayment_amount]}"
end
end
end
TestRun.new.print
#=> self within initialize = #<TestRun:0x007fcccb2ae988>
# self within print = #<TestRun:0x007fcccb2ae988>
# 1 - 224.34
# 2 - 241.5
@transactionsis being defined on the eigenclassTestRun, rather than instances ofTestRun.