Marnen Laibow-Koser wrote:
> Gui Djos wrote:
>> Heya fellas. I've coded C, Java and perl for a while now, but I'm
>> completely new to Ruby (started off today ;) ), and I have an error
>> that's bugging me. Basically, the script is intented to calculate the
>> radices of an equation using the Baskara method.
> 
> And what is the error?  You neglected to say.
> 
>> 
>>  Ive tried working with Float() conversions, and that hasn't given me
>> the right results either. The code is as follows:
>> 
>> 
>> [code]
>> def resolveBaskara(a, b = 1, c = 1)
> 
> camelCase is usually avoided in Ruby; underscore_case is the prevailing 
> style.
> 
>> 
>>   if a == 0
>>     puts "Not a second degree equation.\n\n"
> 
> Why the \n\n?
> 
>>     return
>>   end
>> 
>>   delta = x_1 = x_2 = 0
> 
> This line is 100% unnecessary.  delta, x_1, and x_2 are all assigned to 
> again before they're ever read.
> 
>> 
>>   delta = Float((b ** 2) - (4 * a * c))
> 
> Float() is not needed here.
> 
> Also, even if it were needed, you really shouldn't be using Floats for 
> math.  BigDecimal is a better choice.
> 
>> 
>>   if delta < 0
>>     puts "Negative Delta.\n\n"
> 
> Why the \n\n?
> 
>>     return
>>   end
>> 
>>   x_1 = Float((-1 * b + delta ** 1/2)/2 * a)
> 
> Again, no need for Float().  And why not use sqrt(delta)?
> 
>> 
>> 
>>   if delta == 0
>>     x_2 = x_1
>>   else
>>     x_2 = Float((-1 * b - delta ** 1/2)/2 * a)
> 
> Again, no need for Float().
> 
>>   end
>> 
>>   return x_1,x_2
> 
> You can't return multiple values; you'll have to wrap them in an array 
> (your assignment statement below will still work).
> 
>> end
>> 
>> 
>> print "Give a value for A: "
>> A = gets.chomp
>> print "Give a value for B: "
>> B = gets.chomp
>> print "Give a value for C: "
>> C = gets.chomp
> 
> You probably don't want to use A, B, and C here: capitalized names are 
> reserved for constants (including classes).
> 
>> 
>> x1,x2 = resolveBaskara(A.to_f,B.to_f,C.to_f)
>> 
>>   if x1 != nil
>>     puts "Raizes: \n\tX1: #{x1}"
>>   end
>>   if x2 != nil
>>     puts "\n\tX2: #{x2}"
>>   end
> 
> You don't need != nil here; since nil is a false value, you can just do 
> "if x1".
> 
>> [/code]
>> 
>>  Again, I'm new to this, so forgive any silly mistakes / bad practices.
> 
> Are you developing test-first?  If not, that's a bad practice.
> 
>> I'll learn it when I learn it.
> 
> Best,
> --
> Marnen Laibow-Koser
> http://www.marnen.org
> marnen / marnen.org


 I should point out, mr. Koser, that your attitude does nothing but 
discourage new programmers from learning Ruby. In that sense, as highly 
as you may think of yourself, you act as a negative force in the 
community. You pull it backwards, which, if my impressions on Ruby's 
philosophy are correct, is, to say the least, "very crappy". With all 
your experience in this specific language, sir, you do not seem like a 
"Happy Programmer", and I don't think mr. "Matz" would approve your 
behavior towards newcomers to the creative world of Ruby. Just because 
you're more experienced than others on something, it doesn't mean you 
have the right to step on them.

 I would never bash you for your terrible web designining skills if you 
asked me for help on a web design forum, even knowing you seem to rely 
on them for professional means.

 You did give me some advice on healthy Ruby coding, but I'll show you 
how to teach the same stuff with a better attitude (I've done it with 
other areas, including programming, in which I actually have valuable 
knowledge):

 - try to avoid methodName; use method_name instead
 - there's no need to initialize x_1, x_2 or delta, since they receive 
values before being read
 - BigDecimal tends to be a better choice when doing calculations, 
rather than Float. Plus, if you think about it, you didnt need Float() 
for the above statements ;)
 - dont return multiple values from a method. Its better to do it the 
old way (using arrays or hashes) [just fyi, I had read otherwhise in 
more than one place]
 - avoid using capitalized names, unless youre defining constants


 No, I didn't use the squared-root function. That was my preference. As 
was including the \n\n. I'm not developing a professional application, 
I'm just coding something to get to learn the language's syntax and 
structure, and remember its elements. On doing so, I'll display text on 
my screen the way I want to.

 It's not like you ran the code anyways. I thought the error was 
obvious: the radices found are incorrect (they're not radices to the 
function). Were you to run it, I bet it'd be a lot easier to \b 4 times 
on your keyboard to get rid of 4 bytes you find useless than to write 
most of your post.

 "if x_1" would work? Thanks a lot. It's not like it works on every 
language, right? And it's not like I'm trying to fixate certain aspects 
of the language, and associate them with Ruby in my head anyways.

 To the rest of the community, I'm sorry if my post sounds harsh. It's 
just that I've never encourage attitude such as my kind helper's, and I 
don't think you do either.

 Now, if anyone could tell me the reason why the results are wrong, I'd 
really appreciate it. The healthy Ruby practices will come with 
practice, but I do need to know what's going on here ;)

-- 
Posted via http://www.ruby-forum.com/.