On Thu, Dec 20, 2012 at 7:23 PM, Derrick B. <[email protected]> wrote:
> Robert Klemme wrote in post #1089733:
>>
>> I would choose a completely different approach: I would have a single
>> expression for matching and decide which assignments to make based on
>> the value of one of the capturing groups in the conditional branch:
>>
>> ["1 3/4", "5"].each do |s|
>> puts s
>>
>> if %r{\A(\d+)(?:\s+(\d+)/(\d+))?\z} =~ s
>> w, n, d = $2 ? [$1.to_i, $2.to_i, $3.to_i] : [1, $1.to_i, 0]
>> printf "%4d %4d %4d\n", w, n, d
>> else
>> $stderr.puts "No match #{s}"
>> end
>> end
>>
>> I also spiced the regexp a bit more to be more restrictive.
>
> I am prompting for input, so I would be parsing individual strings and
> not a list of them.
That was just for demonstration purposes.
> Though I will remember that for future static
> testing. Also, I and using the "\S+" to handle negative rational
> numbers: "-1 3/4".
There's still a better way to do that then just match everything non
whitespace. How would you parse ":s9d2++*3h43" as a number?
["1 3/4", "5", '-23', '-23 -4/4'].each do |s|
puts s
if %r{\A([-+]?\d+)(?:\s+([-+]?\d+)/(\d+))?\z} =~ s
w, n, d = $2 ? [$1.to_i, $2.to_i, $3.to_i] : [1, $1.to_i, 0]
printf "%4d %4d %4d\n", w, n, d
else
$stderr.puts "No match #{s}"
end
end
Btw, did I mention that I find your variable assignment weird? I'd rather do
["1 3/4", "5", '-23', '-23 -4/4'].each do |s|
puts s
if %r{\A([-+]?\d+)(?:\s+([-+]?\d+)/(\d+))?\z} =~ s
w, n, d = $2 ? [$1.to_i, $2.to_i, $3.to_i] : [$1.to_i, 0, 1]
printf "%4d %4d %4d\n", w, n, d
else
$stderr.puts "No match #{s}"
end
end
>> If you like readability then why are you using Perl in the first place?
>> :-)
>
> Because Perl is awesome!
Hm... It's been a while that I thought that (but I did actually
once). IMHO Perl has gone over the edge of usefulness long ago.
> That is why I chose Ruby as my next personal
> choice of languages to learn.
Good choice!
> I just completed a quarter of Programming
> Language Concepts where I was introduced to Lisp, but was also required
> to use some Python. I know Python is a fascinatng language, but I am
> not yet a big fan of it. The final assignment was write a class to work
> with fractions. It started as a C++ class, then migrated to Perl (for
> fun), and now Ruby (more fun).
:-)
> I attached the two versions, Ruby and Perl. The Perl version uses a
> "class", also. Feel free to suggest how to make the Ruby version more
> Ruby-ish! My goal is to take more advantage of the OO aspects of Ruby,
> and not just have it look like Perl.
I don't have the time right now but you should definitively use class
Rational. Also your parsing of the command line could be better:
do
print "Fraction 1: "
input = gets or break
f1 = parse_fraction(input)
print "Fraction 2: "
input = gets or break
f2 = parse_fraction(input)
print "Command: "
input = gets or break
case input
when "A"
puts f1 + f2
when "S"
puts f1 - f2
...
else
abort "Invalid command: #{input}"
end
end until cmd == "Q"
puts "Quitting"
You should also not implement #to_string but rather #to_s and then use
string interpolation for constructing your outputs.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en