I need (or want to do something like the following:
when ((/^—++ (.*)/) and (new_record == true))
I’ve tried a lot of variations, but either this just won’t work, or I
haven’t
managed to guess the proper syntax. (I’ve also tried googling and
searching
in the pickaxe(2).)
Aside: Maybe the compound won’t work because one is what the pickaxe
calls a
condition and the other is a comparison? But, I’m not sure which is
which.
If this can’t work, I’ll try (I’ve been trying) a nested if statement,
but
I’ll ask about that in my next post.
If this can’t work, I’ll try (I’ve been trying) a nested if statement, but
I’ll ask about that in my next post.
this can’t work for the simple reason that the objects given at ‘when’
are being sent the === message with the object in ‘case’. In your
‘when’ it’s the result of the stuff in closures.
This generally is the job of ‘if’, just keep in mind that case/when is
for matching, if/else is for conditionals.
One roundabout way exists to make this work though:
class Proc
def ===(obj)
self[obj]
end
end
case ‘foo’
when lambda{|e| new_record and /^—++ (.*)/ === e }
puts :aye_match
else
puts :sorry_you_lose
end
This is generally only advised if your middle name is some form of
(Advent|Dang)erous, I won’t be able to take responsibility for all
resulting damage. But also note that new_record being true or false
makes it simple to work with it without first comparing true/false
with true, the result won’t change, true cannot become any more true.
condition and the other is a comparison? But, I’m not sure which is which.
If this can’t work, I’ll try (I’ve been trying) a nested if statement, but
I’ll ask about that in my next post.
this can’t work for the simple reason that the objects given at ‘when’
are being sent the === message with the object in ‘case’. In your
‘when’ it’s the result of the stuff in closures.
This generally is the job of ‘if’, just keep in mind that case/when is
for matching, if/else is for conditionals.
See Pena’s comment: there is another form of ‘case’ which can be nicely
used to solve this.
case sw
if foo===sw
case
this form is interpreted using if like,
I would reckon fr your post that you may want the second form. So something like,
case
when ((/^—++ (.*)/) and new_record
#…
end
Maybe rather:
case
when new_record && /^—++ / =~ s
Note the matching operator, otherwise $_ will be matched. Note also,
that it is superfluous to match the rest of the string unless the
content need to be further processed. I’d probably also reverse the
order since I assume new_record is a local variable and that test is
likely faster than the regexp matching.
I’ve tried the suggested approaches and variations that I could think
of, and
still no luck.
I’ve attached the following small files in case anyone wants to try to
see if
they can get it to work:
askconvert: the code–at the point in question you will find two
commented out lines and a comment that will be fairly self explanatory
(I
think)
test.txt: a test file
test.aml.txt: a file containing the desired result
utest: a bash script for doing a sort of poor man’s unit test–this
may
not work for you unless you make some modifications (like to the
./askconvert
line)
The unit test works by running the program and then doing a diff between
text.aml (the output from the program) and test.aml.txt. Note that the
current date and time are incorporated in two of the header lines, the
From
and the Date: line, so those will always show up as different. So, I
visually inspect the diff for these two criteria:
the only lines in the diff related to From and Date lines with
different
dates
all titles in the diff should show up as variations of “Primary”
The other thing that will show up is the minor spacing issue mentioned
below.
Someday, I might write code (in that unit test file) to, one way or
another,
ignore those dates so they don’t show up in the diff. (I might process
test.aml and test.aml.std to replace the real date/times with dummys
that are
all the same.)
Note that there is one minor linespace issue remaining in the program
that I
just haven’t been able to resolve so far. It is so minor that I’m going
to
ignore it for now, but, you can spot it if you watch carefully when you
run
the diff.
On Wednesday 17 October 2007 01:46 pm, Randy K. wrote:
On Wednesday 17 October 2007 01:25 pm, Randy K. wrote:
I’ve tried the suggested approaches and variations that I could think of,
and still no luck.
Ok, for the record I got it to work–here’s a little working test
program:
#! /usr/bin/env ruby
new_record = true
line = “—++ tasty”
case
when (new_record and /^—++ (.)/ =~ line)
puts "a title if at the beginning of a record: " + $1
when /^—++ (.)/ =~ line
puts "a level 2 heading if not at the beginning of a record: " + $1
else
puts “neither a title nor a level 2 heading”
end
My mistake was leaving “line” after “case” when I tried to switch to the
alternate form of case statement. Sometimes (or more often I can be
pretty dense.
(And, of course, the other thing I finally did was make a small test
snippet
of the code for experimentation.)
Oops, I had a dumb mistake in one of those two commented out lines in
askconvert. Fixing them doesn’t solve the problem, but if you don’t fix
them
you won’t get the program to work.
Attached is a revised copy of askconvert.
Sorry about that!
Randy K.
On Wednesday 17 October 2007 01:25 pm, Randy K. wrote:
I’ve tried the suggested approaches and variations that I could think of,
and
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.