[SOLUTION] Ruby Quiz #53

  • Thread starter Daniel Sheppard
  • Start date
D

Daniel Sheppard

My solution only acts on one 'line' at a time, by calling enumerating
over the content. However, if you pass in an array with multiple lines
in each index, it can use that too. Matches are based on the punctuation
structure and word count of the lines. This works really well for the
example given, but may not in other situations.

http://members.iinet.net.au/~soxbox/ruby_quiz_53/dryer.rb
http://members.iinet.net.au/~soxbox/ruby_quiz_53/array_sync_enum.rb

The lambda names in the output all suck completely which always makes
the output longer than the input, but I don't have the time to make them
prettier. It would be easy enough to do a find/replace on the lambda
names to make something more useful. I've tried as much as possible to
ensure that the generated output will match the generated input exactly.

It runs in O(n!) time in regards to line length, so putting an essay
through this would take FOREVER. I would have been better off not using
the "generate every regex that matches this line" approach - but I
neglected to throw anything with a decent line-length at it until late
in the piece, and then I couldn't be bothered changing it. Just getting
a list of tokens for each line and then matching up the lines with the
most matching tokens would have sufficed. Doofus.

Here's the output generated for the quiz sample input.

%CREATE_TABLE_ =3D lambda do |arg0|
% "CREATE TABLE `"+arg0+"` ("
%end
%_id_int_11_NOT_NULL_auto_increment_ =3D lambda do ||
% "`id` int(11) NOT NULL auto_increment,"
%end
%_varchar_NOT_NULL_default_ =3D lambda do |arg0,arg1|
% "`"+arg0+"` varchar("+arg1+") NOT NULL default '',"
%end
%_description_text_NOT_NULL_ =3D lambda do ||
% "`description` text NOT NULL,"
%end
%PRIMARY_KEY_id_ =3D lambda do ||
% "PRIMARY KEY (`id`)"
%end
%_TYPE_MyISAM_AUTO_INCREMENT_ =3D lambda do |arg0|
% ") TYPE=3DMyISAM AUTO_INCREMENT=3D"+arg0+" ;"
%end
%_int_11_NOT_NULL_default_0_ =3D lambda do |arg0|
% "`"+arg0+"` int(11) NOT NULL default '0',"
%end
=09<%=3DCREATE_TABLE_["authors"]%>
=09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
=09 <%=3D_varchar_NOT_NULL_default_["firstname","50"]%>
=09 <%=3D_varchar_NOT_NULL_default_["name","50"]%>
=09 <%=3D_varchar_NOT_NULL_default_["nickname","50"]%>
=09 <%=3D_varchar_NOT_NULL_default_["contact","50"]%>
=09 <%=3D_varchar_NOT_NULL_default_["password","50"]%>
=09 <%=3D_description_text_NOT_NULL_[]%>
=09 <%=3DPRIMARY_KEY_id_[]%>
=09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>
=20=20
=09<%=3DCREATE_TABLE_["categories"]%>
=09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
=09 <%=3D_varchar_NOT_NULL_default_["name","20"]%>
=09 <%=3D_varchar_NOT_NULL_default_["description","70"]%>
=09 <%=3DPRIMARY_KEY_id_[]%>
=09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>
=20=20
=09<%=3DCREATE_TABLE_["categories_documents"]%>
=09 <%=3D_int_11_NOT_NULL_default_0_["category_id"]%>
=09 <%=3D_int_11_NOT_NULL_default_0_["document_id"]%>
=09) TYPE=3DMyISAM ;
=20=20
=09<%=3DCREATE_TABLE_["documents"]%>
=09 <%=3D_id_int_11_NOT_NULL_auto_increment_[]%>
=09 <%=3D_varchar_NOT_NULL_default_["title","50"]%>
=09 <%=3D_description_text_NOT_NULL_[]%>
=09 <%=3D_int_11_NOT_NULL_default_0_["author_id"]%>
=09 `date` date NOT NULL default '0000-00-00',
=09 <%=3D_varchar_NOT_NULL_default_["filename","50"]%>
=09 PRIMARY KEY (`id`),
=09 KEY `document` (`title`)
=09<%=3D_TYPE_MyISAM_AUTO_INCREMENT_["14"]%>
#########################################################################=
############
This email has been scanned by MailMarshal, an email content filter.
#########################################################################=
############
 
H

Hugh Sasse

My solution only acts on one 'line' at a time, by calling enumerating
over the content. However, if you pass in an array with multiple lines
in each index, it can use that too. Matches are based on the punctuation

I don't quite understand that -- it seems to
reject {|x| String === x}
knock the strings out of arrays.
structure and word count of the lines. This works really well for the
example given, but may not in other situations.

http://members.iinet.net.au/~soxbox/ruby_quiz_53/dryer.rb
http://members.iinet.net.au/~soxbox/ruby_quiz_53/array_sync_enum.rb

The lambda names in the output all suck completely which always makes
the output longer than the input, but I don't have the time to make them
prettier. It would be easy enough to do a find/replace on the lambda
names to make something more useful. I've tried as much as possible to
ensure that the generated output will match the generated input exactly.

I've not really got into that bit yet, but to use a block for that
is a novel suggestion.
It runs in O(n!) time in regards to line length, so putting an essay
through this would take FOREVER. I would have been better off not using

:) Maybe some duplicate checking up front might help.
the "generate every regex that matches this line" approach - but I
neglected to throw anything with a decent line-length at it until late
in the piece, and then I couldn't be bothered changing it. Just getting
a list of tokens for each line and then matching up the lines with the
most matching tokens would have sufficed. Doofus.

Here's the output generated for the quiz sample input.

%CREATE_TABLE_ = lambda do |arg0|
% "CREATE TABLE `"+arg0+"` ("
%end
%_id_int_11_NOT_NULL_auto_increment_ = lambda do ||
% "`id` int(11) NOT NULL auto_increment,"
%end [...]
<%=CREATE_TABLE_["authors"]%>
<%=_id_int_11_NOT_NULL_auto_increment_[]%>
<%=_varchar_NOT_NULL_default_["firstname","50"]%> [...]
<%=_description_text_NOT_NULL_[]%>
<%=PRIMARY_KEY_id_[]%>
<%=_TYPE_MyISAM_AUTO_INCREMENT_["3"]%>
[...]


I think this is the first one using erb.

Thank you,
Hugh
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top