# Setter scoping issue

**URL:** <https://www.ruby-forum.com/t/setter-scoping-issue/117153>\
**Category:** Ruby\
**Created:** [October 14, 2007, 12:04am UTC](https://www.ruby-forum.com/t/setter-scoping-issue/117153 "2007-10-14T00:04:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [October 14, 2007, 12:04am UTC](https://www.ruby-forum.com/t/setter-scoping-issue/117153/1 "2007-10-14T00:04:37Z")

</div>

So, I had this really cool idea for a syntax.

class Options  
attr\_accessor :something, :something\_else  
end

class Post  
@@options ||= Options.new  
def self.options(&block)  
@@options.instance\_eval &block if block\_given?  
@@options  
end  
end

Post.options.something = “something”  
assert\_equal “something”, Post.options.something

Post.options do  
something = “something”  
something\_else = "something\_else  
end  
assert\_equal “something”, Post.options.something  
assert\_equal “something\_else”, Post.options.something\_else

That’s the idea anyway. A shorthand for scoping setters. If you want  
to set one option, you call the whole method chain. If you want to set  
multiple options, you use a block.

Unfortunately, the setters don’t seem to work in the block without an  
explicit self.

Post.options do  
self.something = “something”  
self.something\_else = "something\_else  
end

…which defeats the entire purpose of the shorthand.

Anybody have any ideas?

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [October 14, 2007, 12:27am UTC](https://www.ruby-forum.com/t/setter-scoping-issue/117153/2 "2007-10-14T00:27:02Z")

</div>

On Oct 13, 2007, at 4:04 PM, James Golick wrote:

> ```
> @@options
> 
> ```
> 
> assert\_equal “something”, Post.options.something  
> Post.options do  
> self.something = “something”  
> self.something\_else = "something\_else  
> end
> 
> …which defeats the entire purpose of the shorthand.
> 
> ## Anybody have any ideas?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

cfp:~ \> ruby a.rb  
cfp:~ \> ruby a.rb  
cfp:~ \> cat a.rb  
require ‘attributes’ ### gem install attributes

class Options  
attributes :something, :something\_else  
end

class Post  
@@options ||= Options.new

def self.options &block  
@@options.instance\_eval &block if block\_given?  
@@options  
end  
end

Post.options.something = “something”  
abort unless “something” == Post.options.something

Post.options do  
something “something”  
something\_else “something\_else”  
end  
abort unless “something” == Post.options.something  
abort unless “something\_else” == Post.options.something\_else

p :success

cfp:~ \> ruby a.rb  
:success

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [October 14, 2007, 12:37am UTC](https://www.ruby-forum.com/t/setter-scoping-issue/117153/3 "2007-10-14T00:37:49Z")

</div>

ara.t.howard wrote:

> On Oct 13, 2007, at 4:04 PM, James Golick wrote:
> 
> > ```
> > @@options
> > 
> > ```
> > 
> > assert\_equal “something”, Post.options.something  
> > Post.options do  
> > self.something = “something”  
> > self.something\_else = "something\_else  
> > end
> > 
> > …which defeats the entire purpose of the shorthand.
> > 
> > ## Anybody have any ideas?
> > 
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> > require ‘attributes’ ### gem install attributes  
> > Post.options do  
> > something “something”  
> > something\_else “something\_else”  
> > end

Thanks a lot. That’s really nice. We lose the equals sign though.  
I’ll gladly keep this, if need be, but I’d really like to keep the  
equals sign.

Is it possible?

---

<div class="post-metadata">

**Author:** ![bbazzarrakk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/47e85d/32.png) [@bbazzarrakk](https://www.ruby-forum.com/u/bbazzarrakk)\
**Post date:** [October 14, 2007, 12:47am UTC](https://www.ruby-forum.com/t/setter-scoping-issue/117153/4 "2007-10-14T00:47:29Z")

</div>

On Oct 13, 2007, at 4:37 PM, James Golick wrote:

> Thanks a lot. That’s really nice. We lose the equals sign though.  
> I’ll gladly keep this, if need be, but I’d really like to keep the  
> equals sign.
> 
> Is it possible?

nope. not without self too. trust me, the open syntax grows on you too

widget.configure {  
width 42  
height 42.0  
color ‘blue’  
}

looks very nice in a config file or class parameterization.

regards.

a @ [http://codeforpeople.com/](http://codeforpeople.com/)
