New to Ruby here and trying to output the contents of an instance for a class I've created. I don't think it should matter, but known_var in the following is being retrieved from an array. The line I am running is simply:
for known_var in @known_variables
puts "known_var is #{known_var}"
The result I am getting is consistently in the format:
resistance = 2200 Ohmsknown_var is #<Variable:0x000001e21047f920>
The class does have a customized to_s definition, which why the first chunk is formatted as it is:
def to_s
if @value == nil
print "#@full_name = 0"
else print "#@full_name = #@value #@unit"
end
end
However, I'm not sure why this is showing up before the "known_var is" part of the string. It looks similar if I specify:
puts "known_var is #{known_var}"
minus the Variable part, which makes sense:
amperage = 12 Aknown_var is
Is there something that should be done differently if I want it to just output the text in the order it's provided in puts: "known_var is XXXXXX"?
I've run some searches to see if I can find an explanation (without luck so far). I can work around it by splitting the puts into two separate lines, but that's not what I'm looking to do, and more importantly, I want to understand why puts is ordering things in the way it is here.
putsis not ordering anything. Let us construct a toy example, and then trace through the execution.Just as you noted, running
testprints[In to_s]foo is 42. How?test: a newFoois constructed, and assigned tofoo"foo is #{foo}"needs to be evaluated in order to pass it as an argument to theputsfunctionfoo.to_sneeds to be evaluatedto_sprints[In to_s]without a newline (because that is whatprintdoes, unlikeputs)to_sreturns"42"(Ruby functions return the last evaluated expression, unless an explicitreturnstatement is executed)foo.to_sevaluates to ("42"), we can evaluate"foo is #{foo}": it is"foo is 42"putsgets executed, with"foo is 42"as its argument"foo is 42"is output, right after the[In to_s]that was output before.As Rajagopalan notes,
to_sshould create a string representation of your object; it is not supposed to output anything. Once I have the string representation, I can decide whether to print it or not. For example, it would be very weird ifbar = foo.to_soutputs anything — I just want to store the text representation offoointo a variable.