CORE - Specialized Attribute Definition

Discussion in 'Ruby' started by Ilias Lazaridis, May 31, 2011.

  1. ruby 1.9

    (this is about the *abilities* or the ruby 1.9 language)

    class Persnon
    attrib name String opt1 opt2
    attrib age Integer
    end

    The main questions:
    a) Can such an "attrib" method be implemented with ruby code?
    b) Can such an "attrib" functionality be implemented on the C-level
    (as extension, not as modification of source)?

    Any examples?

    Please notice the requirements:

    1) "name" and not ":name"
    2) no comma between "name" and "String" and "opt1" ...


    ..
     
    Ilias Lazaridis, May 31, 2011
    #1
    1. Advertisements

  2. Ilias Lazaridis

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    Almost certain this can't be done. You could do something somewhat close to
    that, as seen in the below example. But it relies on hacking method_missing
    and defining constants as methods which catch their call and store them for
    later evaluation, eventually being evaluated when arriving at the attrib
    method.

    However, you would be susceptible to all the method_missing bugs. In fact,
    your example even hit one, because Class.new.respond_to?:)name) is true, it
    wouldn't hit method_missing. This is why in the example, it must be called
    name2. Also, to implement this, you must interfere with the natural flow of
    method missing, so if you actually do invoke a missing method anywhere, then
    this code will think it is part of a signature for some method you're
    passing to attrib.


    module ImDoingThisForTheChallengeEvenThoughItsAHorribleIdea

    def next_meth_sig
    @next_meth_sig ||= []
    end

    def attrib(signature)
    methname, *to_return = signature.reverse
    signature.clear
    define_method(methname) { to_return }
    end

    def method_missing(meth, *args, &block)
    # when empty, args may be [String], [Integer], etc
    # because they get treated as const lookup when doing attrib a Integer
    # but get treated as method call when doing attrib a Integer b
    return next_meth_sig << meth unless next_meth_sig.empty?
    next_meth_sig.concat args << meth
    end

    [String, Integer].each do |const|
    define_method const.to_s do |args|
    next_meth_sig << const
    end
    end

    end



    require 'rspec'
    describe "ImDoingThisForTheChallengeEvenThoughItsAHorribleIdea#attrib" do

    before :each do
    @class = Class.new do
    extend ImDoingThisForTheChallengeEvenThoughItsAHorribleIdea
    end
    end

    context 'when given "attrib name2"' do
    before { @class.instance_eval { attrib name2 } }
    subject { @class.new }
    it { should respond_to :name2 }
    its:)name2) { should == [] }
    end

    context 'when given "attrib name2 String"' do
    before { @class.instance_eval { attrib name2 String } }
    subject { @class.new }
    its:)name2) { should == [String] }
    end

    context 'when given "attrib name2 String opt1 opt2"' do
    before { @class.instance_eval { attrib name2 String opt1 opt2 } }
    subject { @class.new }
    it { should respond_to :name2 }
    its:)name2) { should == [String, :eek:pt1, :eek:pt2] }
    end

    specify '"attrib age Integer" should define #age which returns [Integer]'
    do
    @class.instance_eval { attrib age Integer }
    @class.new.age.should == [Integer]
    end

    it "should work with Illias' example" do
    class Persnon
    extend ImDoingThisForTheChallengeEvenThoughItsAHorribleIdea
    attrib name2 String opt1 opt2
    attrib age Integer
    end
    p = Persnon.new
    p.name2.should == [String, :eek:pt1, :eek:pt2]
    p.age.should == [Integer]
    end

    end
     
    Josh Cheek, May 31, 2011
    #2
    1. Advertisements

  3. Ilias Lazaridis

    Bira Guest

    Just out of curiosity, what exactly do you need the syntax above to
    do, and why isn't attr_accessor sufficient?

    --=20
    Bira
    http://compexplicita.tumblr.com
     
    Bira, May 31, 2011
    #3
  4. Ilias Lazaridis

    Ryan Davis Guest

    I know this is a futile gesture, BUT...

    No, not with your requirements of changing the grammar. If you don't =
    want to code in ruby, go find a language that better suits your needs. =
    If you do, work with it, not against it. What you describe above is =
    simply NOT ruby. Haskell might be a better fit.
     
    Ryan Davis, May 31, 2011
    #4
  5. [...]

    this is far to complex expressed.

    Can you simplify this, e.g. avoiding usage of "rspec"?

    ..
     
    Ilias Lazaridis, Jun 1, 2011
    #5
  6. I think they are able to understand it, it's not that complicated
    (only complicated expressed / coded). But there are some problems:

    The main problem with your suggestion is
    * that any classes to be used must be hardcoded
    * that class constants become functions:

    [String, Integer].each do |const|
    define_method const.to_s do |args|
    next_meth_sig << const
    end
    end

    Additionally, you wrote:

    "However, you would be susceptible to all the method_missing bugs. In
    fact,
    your example even hit one, because Class.new.respond_to?:)name) is
    true, it
    wouldn't hit method_missing. This is why in the example, it must be
    called
    name2."

    It's not a bug. You hit on the Module#name method.

    -

    I understand that such a construct could be implemented as a C-level
    extension (without alteration of the main sources). Can someone pleas
    confirm?

    ..
     
    Ilias Lazaridis, Jun 4, 2011
    #6
  7. Ilias Lazaridis

    Josh Cheek Guest

     
    Josh Cheek, Jun 4, 2011
    #7
  8.  
    Ilias Lazaridis, Jun 4, 2011
    #8
  9. Ilias Lazaridis

    Peter Zotov Guest

    No it couldn't. The feature you want requires altering of the Ruby
    parser
    behavior. No amount of C-level extensions to MRI would help you.
     
    Peter Zotov, Jun 4, 2011
    #9
  10. I understand.

    a) The feature requires alteration of the parser
    b) The parser cannot be altered with a C-level extension

    Thus:

    The Specialized Attribute Definition can be achieved only by a source-
    level modification.

    ..
     
    Ilias Lazaridis, Jun 5, 2011
    #10
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.