Setter scoping issue

Discussion in 'Ruby' started by James Golick, Oct 13, 2007.

  1. James Golick

    James Golick Guest

    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?
    --
    Posted via http://www.ruby-forum.com/.
     
    James Golick, Oct 13, 2007
    #1
    1. Advertising

  2. James Golick

    ara.t.howard Guest

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

    > 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?
    > --
    > Posted via 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/
    --
    share your knowledge. it's a way to achieve immortality.
    h.h. the 14th dalai lama
     
    ara.t.howard, Oct 13, 2007
    #2
    1. Advertising

  3. James Golick

    James Golick Guest

    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/.

    > 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?
    --
    Posted via http://www.ruby-forum.com/.
     
    James Golick, Oct 13, 2007
    #3
  4. James Golick

    ara.t.howard Guest

    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/
    --
    it is not enough to be compassionate. you must act.
    h.h. the 14th dalai lama
     
    ara.t.howard, Oct 13, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frederick Grim

    scoping issue

    Frederick Grim, Sep 12, 2003, in forum: C++
    Replies:
    4
    Views:
    442
    Gianni Mariani
    Sep 14, 2003
  2. James Stroud

    Scoping issue with import

    James Stroud, Feb 28, 2005, in forum: Python
    Replies:
    2
    Views:
    319
    Steve Holden
    Mar 1, 2005
  3. Replies:
    12
    Views:
    755
    Bo Persson
    Aug 15, 2007
  4. John Nagle
    Replies:
    22
    Views:
    660
    Gregory Ewing
    Oct 16, 2010
  5. Pawel Szymczykowski

    require_gem scoping issue?

    Pawel Szymczykowski, Nov 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    107
    Pawel Szymczykowski
    Nov 7, 2006
Loading...

Share This Page