Newbie needs help

Heya fellas. I've coded C, Java and perl for a while now, but I'm
completely new to Ruby (started off today :wink: ), 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.

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)

聽聽if a == 0
聽聽聽聽puts "Not a second degree equation.\n\n"
聽聽聽聽return
聽聽end

聽聽delta = x_1 = x_2 = 0

聽聽delta = Float((b ** 2) - (4 * a * c))

聽聽if delta < 0
聽聽聽聽puts "Negative Delta.\n\n"
聽聽聽聽return
聽聽end

聽聽x_1 = Float((-1 * b + delta ** 1/2)/2 * a)

聽聽if delta == 0
聽聽聽聽x_2 = x_1
聽聽else
聽聽聽聽x_2 = Float((-1 * b - delta ** 1/2)/2 * a)
聽聽end

聽聽return x_1,x_2
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

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
[/code]

Again, I'm new to this, so forgive any silly mistakes / bad practices.
I'll learn it when I learn it.

路路路

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

In Ruby, numbers without a decimal point are treated as integers. The result of dividing an integer by an integer is an integer with any remainder discarded. If either argument is a float, the result is a float without rounding or truncation. The "Float()" method only converts the result of its argument expression to float. Try dividing by 2.0 instead of 2.

You should also look up the relative precedence of the ** and + operator, and while you have the book open, check on how Ruby deals with negative quotients of integers.

-- Bill

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 :wink: ), 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.

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)

  if a == 0
    puts "Not a second degree equation.\n\n"
    return
  end

  delta = x_1 = x_2 = 0

  delta = Float((b ** 2) - (4 * a * c))

  if delta < 0
    puts "Negative Delta.\n\n"
    return
  end

  x_1 = Float((-1 * b + delta ** 1/2)/2 * a)

  if delta == 0
    x_2 = x_1
  else
    x_2 = Float((-1 * b - delta ** 1/2)/2 * a)
  end

  return x_1,x_2
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

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
[/code]

Again, I'm new to this, so forgive any silly mistakes / bad practices.
I'll learn it when I learn it.

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 :wink: ), 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
--
Posted via http://www.ruby-forum.com/\.

Gui Djos wrote:

  x_1 = Float((-1 * b + delta ** 1/2)/2 * a)

Note that "1/2" gives zero in Ruby. William Rutiser explained this and
more ("**" preceed "/" anyway), but I want to make sure you don't miss
this 1/2.

路路路

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

Gui Djos wrote:

if a == 0
   puts "Not a second degree equation.\n\n"

Why the \n\n?

if delta < 0
   puts "Negative Delta.\n\n"

Why the \n\n?

Marnen, that's probably because puts only adds a "\n" if it needs to so it takes "\n\n" to leave a blank line in the output.

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

You can return multiple values just as you have it. What gets returned is actually an array and will permit the kind of parallel assignment you expect. (If you only have one value, it will get the array itself.)

Are you developing test-first? If not, that's a bad practice.

Gui, I have to agree with Marnen on this one. Particularly for this kind of stuff where it is very easy to define both good and bad results.

I'll learn it when I learn it.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

路路路

On Feb 15, 2010, at 9:47 PM, Marnen Laibow-Koser wrote:

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 :wink: ), 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 :wink:
- 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 :wink:

路路路

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

William Rutiser wrote:

In Ruby, numbers without a decimal point are treated as integers. The
result of dividing an integer by an integer is an integer with any
remainder discarded. If either argument is a float, the result is a
float without rounding or truncation. The "Float()" method only converts
the result of its argument expression to float. Try dividing by 2.0
instead of 2.

You should also look up the relative precedence of the ** and +
operator, and while you have the book open, check on how Ruby deals with
negative quotients of integers.

-- Bill

Now that's a useful post. Rob's was nice too, and I appreciate it guys.
I'll have a look at all the info you gave me and try to see through this
one.

路路路

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

Gui Djos wrote:

William Rutiser wrote:
  

In Ruby, numbers without a decimal point are treated as integers. The
result of dividing an integer by an integer is an integer with any
remainder discarded. If either argument is a float, the result is a
float without rounding or truncation. The "Float()" method only converts
the result of its argument expression to float. Try dividing by 2.0
instead of 2.

You should also look up the relative precedence of the ** and +
operator, and while you have the book open, check on how Ruby deals with
negative quotients of integers.

-- Bill
    
Now that's a useful post. Rob's was nice too, and I appreciate it guys. I'll have a look at all the info you gave me and try to see through this one.
  

Thank you. By the way I think I ment ** and / in the remark about precedence, not +. It was late at night.

Have fun with Ruby.

-- Bill

Rob Biedenharn wrote:

Gui Djos wrote:

if a == 0
   puts "Not a second degree equation.\n\n"

Why the \n\n?

if delta < 0
   puts "Negative Delta.\n\n"

Why the \n\n?

Marnen, that's probably because puts only adds a "\n" if it needs to
so it takes "\n\n" to leave a blank line in the output.

Well, yeah. That seemed like an odd place to put it, though.

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

You can return multiple values just as you have it. What gets
returned is actually an array and will permit the kind of parallel
assignment you expect. (If you only have one value, it will get the
array itself.)

That's good to know. Gui, sorry for steering you wrong! I never even
thought to try doing a multiple return like this.

Are you developing test-first? If not, that's a bad practice.

Gui, I have to agree with Marnen on this one. Particularly for this
kind of stuff where it is very easy to define both good and bad results.

My point exactly. Mathematical programming is one area where test-first
development is particularly easy and powerful.

Best,

路路路

On Feb 15, 2010, at 9:47 PM, Marnen Laibow-Koser wrote:

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Gui Djos wrote:
[...]

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

I gave you help and encouragement to use the Ruby language better. I
don't know why you're responding this way.

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.

Matz is active on this list, and I'm sure he has read my posts. If he
doesn't like them, I hope he'll tell me.

Just because
you're more experienced than others on something, it doesn't mean you
have the right to step on them.

It was not my intention to step on you. It was rather my intention to
encourage you to use the Ruby language in a more idiomatic style, and to
give you some pointers on how to do so.

Your response makes me think I shouldn't have bothered.

I would never bash you for your terrible web designining skills

You just did, by saying that. Don't be disingenuous.

if you
asked me for help on a web design forum, even knowing you seem to rely
on them for professional means.

I'm primarily a developer, not a designer. If I asked you for design
help, I would expect you to tell me everything you didn't like about my
designs, and what you would do instead -- in other words, I would expect
you to do exactly what I did.

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 :wink:
- 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

This is exactly what I said.

[...]

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

Tip for the future: if you post here (or in any similar forum) and just
say "it doesn't work", people are not going to be particularly willing
to help you. If you get an error, always describe it.

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.

It's not about hitting backspace a few times. You posted asking for
help, and you said you were new to Ruby, so I thought I'd point you in
the direction of more Ruby-like code.

"if x_1" would work? Thanks a lot. It's not like it works on every
language, right?

Nothing works in every language. That's why we have documentation. :slight_smile:

And it's not like I'm trying to fixate certain aspects
of the language, and associate them with Ruby in my head anyways.

What?

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 :wink:

Best,

路路路

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.

Marnen Laibow-Koser wrote:

Gui Djos wrote:
[...]

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

I gave you help and encouragement to use the Ruby language better. I
don't know why you're responding this way.

Because people hate being told they are morons! :slight_smile:

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.

Matz is active on this list, and I'm sure he has read my posts. If he
doesn't like them, I hope he'll tell me.

With all due respect to both Matz and you, I believe he has more
important things to do than police/moderate this forum/mailing list. Gui
is right, and your tone in your original response was very harsh and
condescending. I do believe it was not meant to be, but that is how it
came across.

I would never bash you for your terrible web designining skills

You just did, by saying that. Don't be disingenuous.

No, he just made an example. He was not actually commenting your
web-design skills.

if you
asked me for help on a web design forum, even knowing you seem to rely
on them for professional means.

I'm primarily a developer, not a designer. If I asked you for design
help, I would expect you to tell me everything you didn't like about my
designs, and what you would do instead -- in other words, I would expect
you to do exactly what I did.

Marnen: I, like you, enjoy the raw, unadorned truth - especially when I
am plain "doing it wrong", the wrong way, with the wrong thought
process. Not many people share this point of view, however, and prefer a
gentle guiding to the right track. In fact, they are usually very happy
to jump right in line. The basic process is : compliment, correct,
compliment.

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 :wink:
- 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

This is exactly what I said.

Yes... But Gui's words sound like advice - yours sound like bashing
someone in the head! :wink:

[...]

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

Wait.. Mathematics? Why would I worry about that? I have _computers_ who
give me the _right_ answer! :stuck_out_tongue: Gui, what's obvious for one person isn't
obvious for someone else.. :slight_smile:

Tip for the future: if you post here (or in any similar forum) and just
say "it doesn't work", people are not going to be particularly willing
to help you. If you get an error, always describe it.

At this point, it's customary to link to ESR's "how to ask smart
questions", isn't it? :slight_smile:

路路路

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

Aldric Giacomoni wrote:

Marnen Laibow-Koser wrote:

Gui Djos wrote:
[...]

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

I gave you help and encouragement to use the Ruby language better. I
don't know why you're responding this way.

Because people hate being told they are morons! :slight_smile:

True. I didn't think I was doing that.

[...]

With all due respect to both Matz and you, I believe he has more
important things to do than police/moderate this forum/mailing list.

I think so too. :slight_smile:

Gui
is right, and your tone in your original response was very harsh and
condescending. I do believe it was not meant to be, but that is how it
came across.

It was not meant to be, and I apologize if it came across that way.

[...]

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

Wait.. Mathematics? Why would I worry about that? I have _computers_ who
give me the _right_ answer! :stuck_out_tongue: Gui, what's obvious for one person isn't
obvious for someone else.. :slight_smile:

Exactly. :slight_smile:

Tip for the future: if you post here (or in any similar forum) and just
say "it doesn't work", people are not going to be particularly willing
to help you. If you get an error, always describe it.

At this point, it's customary to link to ESR's "how to ask smart
questions", isn't it? :slight_smile:

I thought about doing that...

Best,

路路路

--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
--
Posted via http://www.ruby-forum.com/\.