What is a variable defined inside class definition without decorator @ or @@?

In Ruby, such code is legal:

class Aclass
  m = 1
end

but semantically speaking, what will m be, and how is it supposed to be accessed?

Answers


m is just a local variable. It can only be accessed from within the class definition. It could be used to bootstrap the class for instance, but it can't be accessed from anywhere else.

For example:

class Aclass

  m=1
  puts "m is #{m}"

end

That code would be run only once, when you require the file containing that class. This is somewhat analogous to Java's static initialisation blocks.


Need Your Help

Foundation insert progress bar into top-bar

html css progress-bar zurb-foundation

I use the framework Foundation 5, i use the top bar and i want to insert into the top-bar a simple progress bar

Wordpress: fatal error no access on control panel/admin panel

php wordpress error-handling ftp wordpress-plugin

During a clean up of my plugins inside of Wordpress i deactivated the: smart archives reloaded plugin.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.