# Question about rb\_cvar\_set

**URL:** <https://www.ruby-forum.com/t/question-about-rb-cvar-set/85459>\
**Category:** Ruby\
**Created:** [February 13, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/question-about-rb-cvar-set/85459 "2007-02-13T20:15:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 8:15pm UTC](https://www.ruby-forum.com/t/question-about-rb-cvar-set/85459/1 "2007-02-13T20:15:41Z")

</div>

Hi,

Ruby 1.8.5 p12

It looks like there was a prototype change in rb\_cvar\_set() at some  
point in the 1.8.x branch. It now takes a 4th parameter (an int). What  
is the 4th parameter supposed to be?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 14, 2007, 3:18am UTC](https://www.ruby-forum.com/t/question-about-rb-cvar-set/85459/2 "2007-02-14T03:18:23Z")

</div>

On 2/14/07, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> Ruby 1.8.5 p12
> 
> It looks like there was a prototype change in rb\_cvar\_set() at some  
> point in the 1.8.x branch. It now takes a 4th parameter (an int). What  
> is the 4th parameter supposed to be?

Hi Dan,

If you poke your nose into the rb\_cvar\_set() function, you’ll see it’s  
just a warning enabler flag. Specifically, if this argument is  
nonzero, then in verbose mode (-v, -w) you’ll get a warning if you  
modify a class variable from a subclass. As in:

$ ruby -w -e ‘class B; @@x = 1; end; class D \< B; @@x = 5; end’  
-e:1: warning: already initialized class variable @@x

The flag appears to be used to distinguish between cases such as:

class B  
@@x = 1  
end

class D \< B  
@@x = 2 # warning

```
def f
  @@x = 3 # no warning
end

def self.f
  @@x = 4 # no warning
end

```

end

Here, although the last 3 assignments to @@x all modify B’s @@x, the  
ones inside a def are considered “assignment-style”, whereas the “@@x  
= 2” is “declaration-style”. Only the latter generates a warning.

Regards,  
George.

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 14, 2007, 4:05pm UTC](https://www.ruby-forum.com/t/question-about-rb-cvar-set/85459/3 "2007-02-14T16:05:41Z")

</div>

On Feb 13, 7:17 pm, “George O.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Dan,
> 
> ```
> def self.f
> @@x = 4 # no warning
> end
> 
> ```
> 
> end
> 
> Here, although the last 3 assignments to @@x all modify B’s @@x, the  
> ones inside a def are considered “assignment-style”, whereas the “@@x  
> = 2” is “declaration-style”. Only the latter generates a warning.

Ah, thanks George. It looks like you’re supposed to test for the  
RB\_CVAR\_SET\_4ARGS macro to stay backwards compatible, too.

Dan
