CORE - Specialized Attribute Definition

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” …

.

On Tue, May 31, 2011 at 8:05 AM, Ilias L.
[email protected]wrote:

a) Can such an “attrib” method be implemented with ruby code?

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, :opt1, :opt2] }
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, :opt1, :opt2]
p.age.should == [Integer]
end

end

On Tue, May 31, 2011 at 10:05 AM, Ilias L. [email protected]
wrote:

a) Can such an “attrib” method be implemented with ruby code?

.


http://lazaridis.com

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

On May 31, 2011, at 06:05 , Ilias L. wrote:

Any examples?

Please notice the requirements:

  1. “name” and not “:name”
  2. no comma between “name” and “String” and “opt1” …

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.

On 31 , 17:26, Josh C. [email protected] wrote:

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

Any examples?

Please notice the requirements:

  1. “name” and not “:name”
  2. no comma between “name” and “String” and “opt1” …

Almost certain this can’t be done.

ok

You could do something somewhat close to that, as seen in the below example.
[…]

this is far to complex expressed.

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

.

On 1 Ιούν, 11:28, Josh C. [email protected] wrote:

Any examples?
You could do something somewhat close to that, as seen in the below
example.
[…]

this is far to complex expressed.

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

No, because the request itself makes me worried you’d actually use that
code.

This is a public archived group, thus other readers will benefit from
a compact answer, too.

Of course I can try to minimize / summarize the code myself.

.

On Wed, Jun 1, 2011 at 4:15 AM, Ilias L. [email protected]
wrote:

(as extension, not as modification of source)?
ok
code.

I don’t think anyone else has this problem. But if they do, I don’t
think
they’ll be able to understand the solution anyway.

On 1 Ιούν, 18:38, Josh C. [email protected] wrote:

ruby 1.9
b) Can such an “attrib” functionality be implemented on the C-level

No, because the request itself makes me worried you’d actually use that
code.

This is a public archived group, thus other readers will benefit from
a compact answer, too.

Of course I can try to minimize / summarize the code myself.

I don’t think anyone else has this problem. But if they do, I don’t think
they’ll be able to understand the solution anyway.

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?

.

On Sat, Jun 4, 2011 at 5:00 AM, Ilias L. [email protected]
wrote:

[email protected]

  1. “name” and not “:name”
  • that class constants become functions:
    fact,
    I understand that such a construct could be implemented as a C-level
    extension (without alteration of the main sources). Can someone pleas
    confirm?

.


http://lazaridis.com

Based on all the posts you make in here, it sounds like you would be
better
served by writing a DSL. There is simply not a reliable way to do what
you
are asking in this thread, in Ruby.

The book Eloquent Ruby has a short chapter discussing “external” DSLs,
and
how you can use Ruby to write them, it even touches on how you can let
them
embed Ruby into the external DSL, so that you can retain some of the
extensibility of a general purpose programming language, while still
having
the targeted syntax of a DSL. Its methods include a very simple reading
in
of lines, splitting on whitespace, and then interpreting as you desire,
to a
slightly more robust regex based parser, to a considerably more robust,
though still quite simple Treetop (http://rubygems.org/gems/treetop)
based
parser.

Alternatively, you might enjoy
http://gilesbowkett.blogspot.com/2010/03/create-your-own-programming-language.htmlwhich
covers creating a DSL (actually, the language they implement in that
book is general purpose) using a simple regex based lexer, and racc (
http://rubygems.org/gems/racc) to interpret it. Personally, it was over
my
head, and I failed to implement my language, but you might be more
competent
than I am.

So there are two resources for you within the Ruby world. There are
undoubtedly many others outside of it. Either way, I’d suggest choosing
to
buck the system and do your own thing where you have the kind of
versatility
you seem to be looking for, or simply embracing Ruby as it is / was
intended
to be.

2011/6/1 Ilias L. [email protected]

ruby 1.9
b) Can such an “attrib” functionality be implemented on the C-level

.


http://lazaridis.com

No, because the request itself makes me worried you’d actually use that
code.

On Sat, 4 Jun 2011 20:45:30 +0900, Ilias L. wrote:

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

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.

On 4 Ιούν, 16:41, Peter Z. [email protected] wrote:

On Sat, 4 Jun 2011 20:45:30 +0900, Ilias L. wrote:

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

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.

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.

.

On 4 Ιούν, 14:04, Josh C. [email protected] wrote:

On Tue, May 31, 2011 at 8:05 AM, Ilias L. <

end
[…]

The main problem with your suggestion is

  • that any classes to be used must be hardcoded
  • that class constants become functions:
    […]

"However, you would be susceptible to all the method_missing bugs. In
[…]

It’s not a bug. You hit on the Module#name method.
[…]

Based on all the posts you make in here, it sounds like you would be better
[…] - (suggesting processing - not readed completely)

It’s really no necessary that you suggest me how to process.

I’ve placed concrete questions.

If you don’t have the answers, it sometimes better to keep silence.

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

.