From: Robert Klemme Date: 2008-09-12T20:09:13+09:00 Subject: Re: pass by reference in each loop 2008/9/12 Xiong Chiamiov : > Robert Klemme wrote: >> 2008/9/12 Xiong Chiamiov : >>>> That's quite near to what I suggested: just drop all the @a, @b etc. and >>>> stuff everything in an array - or have multiple arrays, one per class of >>>> data (i.e. data which undergoes wml and data which does not). Anyway, >>>> chances are that you'll be able to find an elegant solution without >>>> needing to change the language. >>> >>> I don't think I understood what you were suggesting the first time. If >>> I understand what you're suggesting, it is to have the values themselves >>> in the array, rather than variables pointing *to* those values, yes? >> >> What exactly do you mean by "variable pointing to those values"? > > Well, I'm not quite sure how to explain it. I think you cleared up what > I was thinking right after this part, though: > > Robert Klemme wrote: >> The value >> (object) does not care how many references are around pointing to it. >> With >> >> @x = Object.new >> @y = Object.new >> @a = [@x, @y] >> >> and >> >> @a = [Object.new, Object.new] >> >> there is no difference with regard to array contents. > >> Yep. Problem is, you present just a very tiny piece of your >> application so it is extremely hard for use to suggest an appropriate >> solution. > > Ah, I knew I had part of it online somewhere: > http://production.xyztextbooks.com/wml.php/generator . This is the PHP > version (before I converted it to Rails), so it hasn't had the benefit > of some bugfixes and such, but you can at least *see* what I'm talking > about. > > This page (http://production.xyztextbooks.com/wml.php/generator/syntax) > has some of the basic syntactical things for this. Essentially, there > is an application called Webwork that uses a special dialect of Perl > intermingled with Tex, and I'm created a front-end to problem-creation, > which normally involves writing code that most teachers are not > comfortable with (see http://webwork.maa.org/wiki/SampleProblem1). > > So then, it's just a simple form (using form_tag, I believe), and I grab > all of the request parameters at the top of the script. The question, > answer and answer_explanation all need to be run through a method that > parses the wml (as I'm calling it) into Perl/Tex code appropriate for > the problem, for instance, from ==3/5== to \(frac{3}{5}\). > > As you can see, it's currently only those three fields that I need to > parse; I don't want to use extra resources running everything through > parsing. In the future, though, I see a larger number of fields that > need to be parsed, which is what I'm trying to write for. So, one way to do it would be this: class WhateverYouNameIt NEEDS_PARSING = [ :foo, :bar, ] def initialize @inputs = {} end # code that fetches input from somewhere def parse NEEDS_PARSING.each do |label| @inputs[label] = wml(@inputs[label]) end end end But then again, whether this approach is better ultimately depends on - as I said - whether you more often than not need to treat those inputs uniformly. This I cannot know and in case you are not, then I'd just plain write the statements in code with all variables, i.e. @foo = wml(@foo) @bar = wml(@bar) even if it looks tedious. Verbosity can be better at times because it makes things clearer. Cheers robert -- use.inject do |as, often| as.you_can - without end