Newbie needs help

G

Gui Djos

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.

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

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

William Rutiser

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
 
M

Marnen Laibow-Koser

Gui said:
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)[/QUOTE]

camelCase is usually avoided in Ruby; underscore_case is the prevailing
style.
[QUOTE]
if a == 0
puts "Not a second degree equation.\n\n"[/QUOTE]

Why the \n\n?
[QUOTE]
return
end

delta = x_1 = x_2 = 0[/QUOTE]

This line is 100% unnecessary.  delta, x_1, and x_2 are all assigned to
again before they're ever read.
[QUOTE]
delta = Float((b ** 2) - (4 * a * c))[/QUOTE]

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.
[QUOTE]
if delta < 0
puts "Negative Delta.\n\n"[/QUOTE]

Why the \n\n?
[QUOTE]
return
end

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

Again, no need for Float().  And why not use sqrt(delta)?
[QUOTE]
if delta == 0
x_2 = x_1
else
x_2 = Float((-1 * b - delta ** 1/2)/2 * a)[/QUOTE]

Again, no need for Float().
[QUOTE]
end

return x_1,x_2[/QUOTE]

You can't return multiple values; you'll have to wrap them in an array
(your assignment statement below will still work).
[QUOTE]
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[/QUOTE]

You probably don't want to use A, B, and C here: capitalized names are
reserved for constants (including classes).
[QUOTE]
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[/QUOTE]

You don't need != nil here; since nil is a false value, you can just do
"if x1".
[QUOTE]

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,
 
R

Rob Biedenharn

Why the \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.
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.


-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
G

Gui Djos

Marnen said:
Gui said:
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)[/QUOTE]

camelCase is usually avoided in Ruby; underscore_case is the prevailing
style.
[QUOTE]
if a == 0
puts "Not a second degree equation.\n\n"[/QUOTE]

Why the \n\n?
[QUOTE]
return
end

delta = x_1 = x_2 = 0[/QUOTE]

This line is 100% unnecessary.  delta, x_1, and x_2 are all assigned to
again before they're ever read.
[QUOTE]
delta = Float((b ** 2) - (4 * a * c))[/QUOTE]

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.
[QUOTE]
if delta < 0
puts "Negative Delta.\n\n"[/QUOTE]

Why the \n\n?
[QUOTE]
return
end

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

Again, no need for Float().  And why not use sqrt(delta)?
[QUOTE]
if delta == 0
x_2 = x_1
else
x_2 = Float((-1 * b - delta ** 1/2)/2 * a)[/QUOTE]

Again, no need for Float().
[QUOTE]
end

return x_1,x_2[/QUOTE]

You can't return multiple values; you'll have to wrap them in an array
(your assignment statement below will still work).
[QUOTE]
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[/QUOTE]

You probably don't want to use A, B, and C here: capitalized names are
reserved for constants (including classes).
[QUOTE]
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[/QUOTE]

You don't need != nil here; since nil is a false value, you can just do
"if x1".
[QUOTE]

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,


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 ;)
 
G

Gui Djos

William said:
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.
 
A

Albert Schlef

Gui said:
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.
 
W

William Rutiser

Gui said:
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
 
M

Marnen Laibow-Koser

Rob said:
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.
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.
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,
 
M

Marnen Laibow-Koser

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

Best,
 
A

Aldric Giacomoni

Marnen said:
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! :)
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.
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.
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 ;)
- 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! ;-)
[...]
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! :p Gui, what's obvious for one person isn't
obvious for someone else.. :)
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? :)
 
M

Marnen Laibow-Koser

Aldric said:
Marnen said:
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! :)

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

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

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

I thought about doing that...

Best,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,955
Messages
2,570,117
Members
46,705
Latest member
v_darius

Latest Threads

Top