Calling Instance Variables Without @

Can you omit the @ when calling instance variables

attr_accessor :foo is basically just a macro that generates two methods (a getter and a setter) for you:

def foo
@foo
end

def foo=(foo)
@foo = foo
end

Can you omit the @ when calling your instance variable? Kind of – calling the instance variable name without the @ means you are calling the instance method generated by attr_accessor instead of calling the instance variable. This works as long as the getter method is not overridden or extended.

But when you would try to set an instance variable without the @, it would not work that way, because then Ruby would set a local variable with that name. To set the instance variable through the instance method generated by attr_accessor you need to write self. (or another receiver) instead of the @:

@foo = 'bar'      # assigns 'bar' to the instance variable `@foo`
foo = 'bar' # assigns 'bar' to a local variable `@foo`

But to use the setter method generated by attr_accessor:

self.foo = 'bar'  # passes 'bar' to the instance method `foo=`

Why can I access a private instance variable without a getter method?

Access Modifiers

Source https://www.geeksforgeeks.org/access-modifiers-java/
You are accessing it from the same class
Check this Table Out..

How to loop over class instance variables without calling methods?

All functions can be called. So, you just need to filter those out:

def info(self):
print('\n'.join("%s: %s" % item
for item in vars(self).items()
if not hasattr(item, '__call__')
))

This will output only the non-callable attributes of the class.

Is it necessary for instance methods to access instance variable in java?

sum is an instance method here, because it's not static and requires an instance of the object. You create your instance here:

Main ob = new Main();

In this particular case sum could indeed be made static, slightly simplifying the code by not requiring an instance.

I have read that if a method is an instance method it should access instance variable

I suspect what you were reading is suggesting that if a method doesn't interact with the instance at all then it probably should be static. There may be mention of the term "pure function" in the text somewhere.

I wouldn't go so far as to say that all potentially static methods everywhere should be made static as a universal rule. It really comes down to the semantics of the object itself. Since the object you have here has very little semantic context, this one tiny example could easily go either way.

But suppose you expanded your object to also include methods for subtract, multiply, divide, etc. As the object expands, suppose one or more of those additional methods did use instance variables. It would be a jarring experience to have an object with multiple semantically-similar methods, some of which are static and some of which are not.

Rather than focus on any particular rule that someone gives you, focus on what you are intending to build as your object. If you think that it should be static, make it as such. If you feel that it shouldn't, then don't. If you're unsure, then as an exercise implement both and see which you prefer in that particular case.

Looking to the future plans for what you're intending to build is important, because the more code you have relying on your object throughout the application the harder it will be to change from static to instance or vice-versa.

How to get private variables of instance object without calling method

You can use inspect for p and to_s for puts

class Foo
def initialize(arg)
@bar = arg
end
def inspect
@bar
end
def to_s
@bar
end
end

f = Foo.new('test')
puts f #=> "test"
p f #=> "test"

Access an instance variable without using the @ sign

This line attr_reader :unique, :index created a getter for the attribute:

def unique
@unique
end

what you see in the line unique.size - 1 is a method call to the getter, then accesing the size property of it.

How methods access instance variable in java

this keyword is optional in java. In this example you can actullay use this keyword also. Only scenario that you will need to use it mandatary is when referring to a field which is named the same both locally and globally.

example:

class Test
{
private int size; //global

public void method(int size)
{
this.size = size; //this.size refers to the declaration within the class, not the method local variable

}
}


Related Topics



Leave a reply



Submit