Ruby实例变量是否有访问修饰符?

时间:2022-08-11 22:30:46

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

一个叫做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?

在Java中,有访问修饰符。如果getter方法以其变量命名,Ruby如何使用访问修饰符?

2 个解决方案

#1


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

一是你从来不“调用”变量。Ruby表达式

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.

然后有一个名为variable的方法和一个名为@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:

Ruby places只在方法*问修饰符公共、受保护和私有,而不是在变量上。对变量的访问控制实际上没有意义,因为它们只在对象的方法中被引用,从来没有前缀!换句话说,你不能这样写:

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.

其中var是方法的名称。访问控制只适用于该方法。

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!

希望这有助于消除一些可以理解的困惑!

#2


2  

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

有几种方法可以使Ruby中的方法成为私有的。

Use private to make all later methods private:

使用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

#1


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

一是你从来不“调用”变量。Ruby表达式

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.

然后有一个名为variable的方法和一个名为@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:

Ruby places只在方法*问修饰符公共、受保护和私有,而不是在变量上。对变量的访问控制实际上没有意义,因为它们只在对象的方法中被引用,从来没有前缀!换句话说,你不能这样写:

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.

其中var是方法的名称。访问控制只适用于该方法。

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!

希望这有助于消除一些可以理解的困惑!

#2


2  

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

有几种方法可以使Ruby中的方法成为私有的。

Use private to make all later methods private:

使用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