On Fri, Dec 21, 2012 at 7:19 PM, Derrick B. <lists / ruby-forum.com> wrote:
> Robert Klemme wrote in post #1089807:

>> 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
>
> I will have to search for "weird" in recent posts.  haha!  I do not
> doubt it, but for my C++ to Perl to Ruby translation, it was
> seamless.

:-)  What I meant by weird was that I would have the first variable
always store the integer number, the second the numerator and the
third the denominator.  With your numbering scheme you need to
evaluate the third value against 0 to detect what kind of number you
have.  With my scheme you can treat all parsed input equally and you
won't have a division by zero (unless the user entered 0 as last
number).

> As I stated as an edit in a previous post, this started as an assignment
> to create a C++ class that handles fractions, so I then created a Perl
> "class", and now a Ruby class.  So, using "require rational", or "use
> bigrat" in Perl, would have defeated the purpose.  I do plan on learning
> more about Ruby, and Perl, add-ons.

Ah, yes of course that makes a whole lot of sense.  I must have
forgotten in between.

Btw, I have a blog posting about creating numerical classes in Ruby:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

You might want to read the previous article first:
http://blog.rubybestpractices.com/posts/rklemme/018-Complete_Class.html

> Thank you all for the input!  I really appreciate being able to post
> code and have quality responses.  I am transitioning from a career
> (nearly 20 years) in IT, to going to school to get my BS in CS, to then
> continue as a programmer.

Good!  Please make sure that you do not miss the part about algorithms
and data structures.  I consider that one of the more important parts
in CS.

> I'll try not to be a stranger as long as my code is to be considered
> strange.  :)

:-)

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/