RPN Calculator Instance method called on calculator instead of @calculator

In Rpn calculator a new instance is created @calculator.
Usually instance methods are called on instance.but in rpn calculator i see in spec that the instance methods are called on calculator instead of @calculator.
Why so?
Is the attr_accessor :calculator helping to call that way?
If so how?
Could anyone explain this to me?
TIA

Nats

attr_accessor :calculator allows for use of both the getter and setter methods to access calculator reference variable.

@calculator can both act as a both getter and setter, meaning you can directly alter the object reference and the instance variable references contained in the calculator variable.

Without the @ in calculator, you are only using the getter method. You can only read the object referenced by the calculator variable and the instance variable references it contains. You cannot alter the data referenced. The changes made will revert once the method it was called in ends.

Add this snippet to the bottom of your 00_rpn_calculator.rb file and run it. You will find the assignment in My_Class#getter on the calc object reference is only temporary and reverts once the method is over, while the assignment in My_Class#setter is permanent.

It also works on repl.it.

class MyClass
	attr_accessor :calc

	def initialize
		@calc = RPNCalculator.new
	end

	def getter
		calc = 22
		puts calc.to_s
	end
	
	def setter
		@calc = 22
		puts @calc.to_s
	end
end

obj = MyClass.new
obj.getter
puts obj.calc.to_s
obj.setter
puts obj.calc.to_s

@nathashas1
To go into a little more detail about Rspec, let’s take a closer look at the following lines:

describe RPNCalculator do
  attr_accessor :calculator

  before do
    @calculator = RPNCalculator.new
  end

  it "adds two numbers" do
    calculator.push(2)
    calculator.push(3)
    calculator.plus
    expect(calculator.value).to eq(5)
  end

Our first line after telling Rspec that we’ll be looking at the RPNCalculator does exactly what @zweih says: creates a getter and setter method.

Our next line, the one that starts with “before”, tells Rspec that before every single test (the lines starting with “it”) that it should create or update it’s instance variable named @calculator to point to a brand new instance of RPNCalculator.

Finally, in the test itself, we are using the setter method to push numbers and operations onto our calculator and then our getter method to check it’s value.

Well go into more detail about how to read and write Rspec tests in week 2 :slight_smile:

PS: I noticed that in @zweih’s response that he talked about using @calculator as a getter method. This is slightly incorrect as @calculator isn’t a method, but rather a variable. We use our getter and setter methods to be clear about the fact that we are modifying and referencing that variable.

Thank you @zweih and @Mmartinez.Appacademy for explaining it to me.Its more clear to me now.
So does Rspec work a little different than Ruby?
Because the same method does not work in RePl for Ruby.

When i write all the Ruby code for RPN Calculator including attr_accessor :calculator and write the below code to run it

@calculator = RPNCalculator.new
calculator.push(2)

calculator.push(3)
calculator.plus

It says calculator is not defined.Do you mean @calculator?

And when i run this :

@calculator.push(2)
@calculator.push(3)
@calculator.plus
Its working.

So does Rspec work differently than Ruby.?

Rspec is a Ruby Gem that you can think of as an add on in order to run tests- It’s built on Ruby, but it follows slightly different rules and syntax.

Are you running the code
@calculator = RPNCalculator.new calculator.push(2)
inside or outside your Calculator class? Also, is this on Repl.it or is this in your command line?

@Mmartinez.Appacademy
I was running this outside the class in the Repl.

Ah. So you shouldn’t be setting a variable with the @ symbol when you aren’t in a class because it is used to refer to instance variables.

Your code isn’t running because you’re setting RPNCalculator.new to a variable named @calculator but you’re calling a method on an undefined variable named calculator.

attr_accessor is a method that shoul only be used inside classes.

Got it.Thank you for explaining.