CORE - Specialized Attribute Definition

I

Ilias Lazaridis

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


..
 
J

Josh Cheek

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

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" ...
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
 
B

Bira

ruby 1.9

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

class Persnon
=A0attrib name =A0String opt1 opt2
=A0attrib 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" ...


.

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
 
R

Ryan Davis

class Persnon
attrib name String opt1 opt2
attrib age Integer
end
=20
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)?
=20
Any examples?
=20
Please notice the requirements:
=20
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.
 
I

Ilias Lazaridis

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

(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" ...

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"?

..
 
I

Ilias Lazaridis

2011/6/1 Ilias Lazaridis <[email protected]>
[Note:  parts of this message were removed to make it a legal post.]
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" ...
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"?
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?

..
 
J

Josh Cheek

2011/6/1 Ilias Lazaridis <[email protected]>
[Note: parts of this message were removed to make it a legal
post.]
@lazaridis.com
wrote:
(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" ...
Almost certain this can't be done.

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.

I don't think anyone else has this problem. But if they do, I don't thi= nk
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?

.
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-langua=
ge.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 competen=
t
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 versatilit=
y
you seem to be looking for, or simply embracing Ruby as it is / was intende=
d
to be.
 
I

Ilias Lazaridis

2011/6/1 Ilias Lazaridis <[email protected]>
[Note:  parts of this message were removed to make it a legal post.]
@lazaridis.com
wrote:
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 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).

..
 
P

Peter Zotov

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

Ilias Lazaridis

 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.

..
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top