class Human
@core = "heart"
def cardiovascular
arr = ['heart','blood','lungs']
core = @core
end
end
Is the only way I am able to access @core directly by using this:
Human.instance_variable_get(:@core) #=> "heart"
My understanding is that an instance variable is accessible from anywhere within the scope of the Class.
I am able to access the method by: Human.new.cardiovascular and I expect the return to be "heart" but the return I get is nil
- How come I can't access the instance variable as
Human.coreorHuman.new.core? - Why does the
Human.new.cardiovascularreturnnil? (Shouldn't core == @core?)
Update
After putting @core within an initialize block I see the following output in IRB:
Human.new
=> #<Human:0x2f1f030 @core="heart">
This makes sense as it's now available to the entire Class, but how do I access the specific instance variables within the initialize block? Meaning, how do I get just: @core without calling the cardiovascular method in this case?
attr_accessor :coreAllows me to access@coreasHuman.new.core