Mykhailo Rybak
Mykhailo Rybak

Reputation: 173

is there a way in Ruby to initialize variables which starts from *underscore* eg. _variable_name

i believe Ruby supports this by default. i need to create bunch of class variables, but don't want to override already defined variables and i don't know which of them will have been defined. I expect something similar to class_attribute but it should create variable which starts with underscore.

Upvotes: 0

Views: 129

Answers (2)

prcu
prcu

Reputation: 1013

ActiveSupport has attr_internal. Here is question on it: what is attr_internal used for

For class attribute just use class << self.

For plain ruby you can use class_attribute to define accessors with _ in the begin, and then add aliases.

Upvotes: 1

Joel Brewer
Joel Brewer

Reputation: 1652

Yes. You can begin a variable with an underscore. For example:

_hello = "_hello"
hello = "hello"

puts _hello
#=> "_hello"

puts hello
#=> "hello"

Upvotes: 0

Related Questions