2

So what I learned is that when I call a variable on an object e.g.

my_object.variable

A method called variable is returning the value of variable

def variable
   @variable
end

In Java, there are access modifiers. How can Ruby make use of access modifiers if getter methods are named after their variables?

0

2 Answers 2

7

First, there is a bit of terminology to clean up in the question before answering it.

One is that you never "call" variables. The Ruby expression

my_object.variable

is a method call. There is no such thing as a variable call. You call methods, not variables. Even if the method is named variable. :)

The second is if you did define the method like this

def variable
   @variable
end

either directly or by saying

attr_reader :variable

Then you have a method named variable and a variable named @variable.

Now to answer the question.

Ruby places access modifiers public, protected, and private on methods only, and not on variables. Access controls on variables don't really make sense, because they only be referenced within an object’s methods, and never with a prefix! In other words, you can never write this:

obj.@var

That's just a syntax error. You can write

obj.var

where var is the name of a method. And the access controls apply to that method only.

The fact that you may be making variables and methods with the same name (except for the @) actually doesn't matter. Only methods have access controls.

Hope that helps clear up some understandable confusion!

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Can I say "call variable" in Java when I do my_object.variable? I understand why I cannot in Ruby.
If you take "call" to mean "access" then yes, it will be understood by most people. But given that we generally speak of "function calls" and "method calls" it is probably best to avoid the term when speaking of variables. But people will understand you in Java, though I think "access" or "read" is better for variables. You call methods but read variables. As you noticed, you really can't say it in Ruby. :) Ruby and Java are indeed quite different here.
Technically speaking, instance variables belong to objects (aka instances), not classes. An instance variable can't be seen from a different object, not a different class. (E.g. it can be seen from a method in a subclass or superclass.)
Very true, and a worthy observation. I meant to say that you can’t write code that refers to instance variables in the source code region outside of the class, but I could have found a better way to say that. I will rewrite the answer.
3

There are several ways to make a method private in Ruby.

Use private to make all later methods private:

class Foo
  def public_method
  end

private

  def private_method
  end
end

Or make a method private after you defined it:

class Foo
  def public_method
  end

  private :public_method # public_method is now private
end

Or - since the method definition returns a symbol too - this also works:

class Foo
  private def method_name
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.