Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Compound conditionals in case when statements? Syntax?

Thread Tools

Compound conditionals in case when statements? Syntax?

Randy Kramer
Posts: n/a
Content-Type: Multipart/Mixed;

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

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 Kramer

On Wednesday 17 October 2007 01:25 pm, Randy Kramer wrote:
> I've tried the suggested approaches and variations that I could think of,

> still no luck.

Content-Type: application/x-ruby;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;

#! /usr/bin/env ruby

# This is iter(ation)-4 (in progress) of ruby program askconvert to convert (initially) askRhk03 formatted files to askRhk04 format. (Later versions may perform additional conversions.) This version does work only for the hard coded conversion of file test.txt to test.aml. Iter-5 will presumably address getting the filenames from the command line. Iter-4 is sort of a break--code cleanup and maybe some comments--much as it is against my religion, I'm sort of trying to see to what extent this is (or can be made) "self-documenting"--I do have plenty of documentation (some pretty rough) in other files like"test.aml", "w") do |f|
first_record = true
new_record = false
titles ="test.txt").each do |line|
case line
when /^\x80\x81\x82\x83$/
new_record = true
# when new_record && /^---\+\+ (.*)/ =~ line
# titles.push $1.strip
# the previous two lines could replace the next five lines if the previous two lines worked (or I could change the case statement to an if, elsif, elsif ... (or whatever)--case just seemed more "elegant" somehow--I'll wait and see if anybody can show me how to get those two lines to work.
when /^---\+\+ (.*)/
if new_record
titles.push $1.strip
f.puts line.rstrip + "\n" end
if new_record then
# I could take the next 14 lines and "move" them to a module / method named something like "process_header"--I'd probably also learn something by doing so. I think I'm "learned out" for the moment.
unless first_record then f.puts "morF\n\n"
else first_record = false end
if titles.length == 3 then
title = titles.pop
title = titles.shift end
f.puts "From \"" + title + "\" " + %x{date -u +"%a %b %d %T %Z %Y"} + "Date: " + %x{date +"%D %H:%M %P %Z"} + "Subject: " + title + "\n"
unless titles.empty?
f.puts "\n"
titles.each { |title| if title.strip.length > 0 then f.puts "T: " + title + "\n" end}
f.puts "\n" end
new_record= false
f.puts line.rstrip + "\n" end
f.puts "morF\n"


Reply With Quote
Randy Kramer
Posts: n/a
On Wednesday 17 October 2007 01:46 pm, Randy Kramer wrote:
> On Wednesday 17 October 2007 01:25 pm, Randy Kramer 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"

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
puts "neither a title nor a level 2 heading"

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

Randy Kramer

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# to conversion for "?" conditionals .Net Sports ASP .Net 15 02-03-2006 04:11 PM
XSD: ways to deal with conditionals on attribute value? XML 1 12-30-2005 09:10 PM
Newbie question about formatting long conditionals Barney Frank Python 3 07-29-2004 10:04 AM
Conditionals & paths Mustafa Aydin XML 0 04-21-2004 05:25 PM
Conditionals in switch 'case' labels Mark Anderson Javascript 4 02-26-2004 11:07 PM