From: Josh Cheek Date: 2011-06-04T20:04:44+09:00 Subject: Re: CORE - Specialized Attribute Definition --90e6ba53ac3442f0b304a4e0d692 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Sat, Jun 4, 2011 at 5:00 AM, Ilias Lazaridis wrote= : > On 1 =CE=99=CE=BF=CF=8D=CE=BD, 18:38, Josh Cheek w= rote: > > On Wed, Jun 1, 2011 at 4:15 AM, Ilias Lazaridis > wrote: > > > On 1 =CE=99=CE=BF=CF=8D=CE=BD, 11:28, Josh Cheek wrote: > > > > 2011/6/1 Ilias Lazaridis > > > > > On 31 =C3=8C=C3=9C=C3=BA=C3=AF=C3=B2, 17:26, Josh Cheek wrote: > > > > > > [Note: parts of this message were removed to make it a legal > post.] > > > > > > > > On Tue, May 31, 2011 at 8:05 AM, Ilias Lazaridis < > > > il...@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 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 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? > > . > > -- > 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-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. --90e6ba53ac3442f0b304a4e0d692--