class-variables

Constants or class variables in ruby?

房东的猫 提交于 2019-11-30 07:49:14
I've been programming in Ruby for a few months now, and I'm wondering when it is appropriate to use constants over class variables and vice versa. (I'm working in Rails, thinking about constants in models). class Category TYPES = %w(listing event business).freeze end OR class Category @@types = %w(listing event business).freeze cattr_reader :types end Are there circumstances where one is preferable to another? Or is it just a matter of taste/style? The main thing is that by using the CONSTANT notation, you're making it clear to the reader. the lower case, frozen string gives the impression is

Enforcing Class Variables in a Subclass

懵懂的女人 提交于 2019-11-29 18:10:53
问题 I'm working on extending the Python webapp2 web framework for App Engine to bring in some missing features (in order to make creating apps a little quicker and easier). One of the requirements here is that each subclass needs to have some specific static class variables. Is the best way to achieve this to simply throw an exception if they are missing when I go to utilise them or is there a better way? Example (not real code): Subclass: class Bar(Foo): page_name = 'New Page' page_name needs to

Where are static class variables stored in memory?

萝らか妹 提交于 2019-11-29 17:47:39
问题 This is a follow-up question to How are static arrays stored in Java memory? . So global variables in C/C++ are stored in the static data segment of memory. But what about static class variables in Java/C++? It can't be the static data segment because you don't know what/how many classes are going to be referenced throughout the duration of your program (because of reflection). It's definitely not the stack because that makes no sense. Storing it on the heap is also kind of iffy. 回答1: In Java

Reference class variable in a comprehension of another class variable

假装没事ソ 提交于 2019-11-29 07:13:05
This may be a simple question, but I'm having trouble making a unique search for it. I have a class that defines a static dictionary, then attempts to define a subset of that dictionary, also statically. So, as a toy example: class example(object): first_d = {1:1,2:2,3:3,4:4} second_d = dict((k,first_d[k]) for k in (2,3)) This produces NameError: global name 'first_d' is not defined How should I be making this reference? It seems this pattern works in other cases, eg: class example2(object): first = 1 second = first + 1 A basic list comprehension has the following syntax [expression for var in

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives “true”, and @@wah.nil? gives error again?

半腔热情 提交于 2019-11-29 01:38:36
Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for #<Object:0x3794c> from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized class variable @@wah in Object from (irb):3 why the instance variable treated differently than a local and class variable? In Ruby, most uninitialized or even non-existing variables evaluate to nil . This is true for local variables, instance variables and global variables: defined? foo #=> nil local_variables #=> [] if false foo = 42 end defined? foo #=>

why are java constants declared static?

陌路散爱 提交于 2019-11-29 01:03:06
Why are java constants declared static ? class Foo{ static final int FII = 2 ; } In this I understand the use of final? Buy why does it have to be static? Why should it be a class variable, and not an instance variable? If it could vary by the instance of a class, then it's clearly not a constant . What would it mean to get a different value of pi for each instance of Math (not that Math even allows instances to be constructed)? Or a different case insensitive ordering for each instance of String ? If a constant is not static, Java will allocate a memory for that constant in every object of

How can Ruby's attr_accessor produce class variables or class instance variables instead of instance variables?

偶尔善良 提交于 2019-11-28 04:09:08
If I have a class with an attr_accessor , it defaults to creating an instance variable along with the corresponding getters and setters. But instead of creating an instance variable, is there a way to get it to create a class variable or a class instance variable instead? Like this: class TYourClass class << self attr_accessor :class_instance_variable end end You can look at this as opening the metaclass of the class (of which the class itself is an instance) and adding an attribute to it. attr_accessor is a method of class Class , it adds two methods to the class, one which reads the instance

Create module variables in Ruby

放肆的年华 提交于 2019-11-28 03:31:15
Is there any way to create a variable in a module in Ruby that would behave similar to a class variable? What I mean by this is that it would be able to be accessed without initializing an instance of the module, but it can be changed (unlike constants in modules). Ruby natively supports class variables in modules, so you can use class variables directly, and not some proxy or pseudo-class-variables: module Site @@name = "StackOverflow" def self.setName(value) @@name = value end def self.name @@name end end Site.name # => "StackOverflow" Site.setName("Test") Site.name # => "Test" If you do not

Is Rails shared-nothing or can separate requests access the same runtime variables?

痴心易碎 提交于 2019-11-27 19:47:36
PHP runs in a shared-nothing environment, which in this context means that every web request is run in a clean environment. You can not access another request's data except through a separate persistence layer (filesystem, database, etc.). What about Ruby on Rails? I just read a blog post stating that separate requests might access the same class variable. It has occurred to me that this probably depends on the web server. Mongrel's FAQ states that Mongrel uses one thread per request - suggesting a shared-nothing environment. The FAQ goes on to say that RoR is not thread safe, which further

In Ruby, why after starting irb, foo.nil? says undefined error, and @foo.nil? gives “true”, and @@wah.nil? gives error again?

旧时模样 提交于 2019-11-27 16:05:01
问题 Same in Ruby 1.8.7 and 1.9.2: $ irb ruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo' for #<Object:0x3794c> from (irb):1 ruby-1.8.7-p302 > @bar.nil? => true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized class variable @@wah in Object from (irb):3 why the instance variable treated differently than a local and class variable? 回答1: In Ruby, most uninitialized or even non-existing variables evaluate to nil . This is true for local variables, instance variables