# Newbie Question

**URL:** <https://www.ruby-forum.com/t/newbie-question/130115>\
**Category:** Ruby\
**Created:** [February 25, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/newbie-question/130115 "2008-02-25T15:18:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 3:18pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/1 "2008-02-25T15:18:50Z")

</div>

Help I am completely new to ruby and to OO programming. Why-does my code  
not compile??

class Quadratic

```
def findroots()
    b = (ARGV[0])
    c = (ARGV[1])
    $stdout.print("You entered " , ARGV[1], ARGV[0], "\n")
    discriminant = b * b - 4.0 * c
    sqroot = Math.sqrt(discriminant)

    root1 = (-b + sqroot) / 2.0
    root2 = (-b - sqroot) / 2.0

    $stdout.print(root1)
    $stdout.print(root2)
end

```

end

if **FILE** == $0  
qf = Quadratic.new  
qf.findroots  
end

---

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 3:34pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/2 "2008-02-25T15:34:08Z")

</div>

On Mon, Feb 25, 2008 at 3:18 PM, Christian Rubio  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> sqroot = Math.sqrt(discriminant)
> 
> ```
> 
> if **FILE** == $0  
> qf = Quadratic.new  
> qf.findroots  
> end

Hi,

First, please, next time post the exact error message. You’ll save  
people that want to help you some investigation.

If the folowing message is similar to your error:  
W:/var/x.rb:7:in `*': can't convert String into Integer (TypeError) from W:/var/x.rb:7:in `findroots’  
from W:/var/x.rb:21

then you’re trying to multiply numbers by strings.  
Replace

> ```
> b = (ARGV[0])
> c = (ARGV[1])
> 
> ```

by  
b = ARGV[0].to\_f  
c = ARGV[1].to\_f  
to\_f will convert string to float. For more info, read the docs of  
String#to\_f and possibly the whole String class  
(e.g. using RI or [RDoc Documentation](http://ruby-doc.org/core/))

If the message is different, please post it and will see.

Jano

---

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 3:53pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/3 "2008-02-25T15:53:54Z")

</div>

Jano S. wrote:

> > ```
> > c = (ARGV[1])
> > 
> > ```
> 
> Jano

Hi Thanks for your help and directions. I made the above substitutions  
and now I get the following error

% ruby quadratic.rb 1.0 1.0  
You entered 1.0 1.0  
quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt (Errno::EDOM) from quadratic.rb:17:in`findroots’  
from quadratic.rb:30  
Here is my program,

#!/usr/bin/env ruby

###########################

## 

## program that solve quadratic equation

## 

###########################

class Quadratic

```
def findroots()
    b = (ARGV[0].to_f)
    c = (ARGV[1].to_f)
    $stdout.print("You entered " , ARGV[1]," ", ARGV[0], "\n")
    discriminant = b * b - 4.0 * c
    sqroot = Math.sqrt(discriminant)

    root1 = (-b + sqroot) / 2.0
    root2 = (-b - sqroot) / 2.0

    $stdout.print(root1)
    $stdout.print(root2)
end

```

end

if **FILE** == $0  
qf = Quadratic.new  
qf.findroots  
end

---

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 4:03pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/4 "2008-02-25T16:03:18Z")

</div>

On Mon, Feb 25, 2008 at 3:53 PM, Christian Rubio  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > If the message is different, please post it and will see.  
> > (Errno::EDOM)  
> > from quadratic.rb:17:in `findroots’  
> > from quadratic.rb:30

That means you passed a number that has no real square root, for example  
-1.  
Refresh your high school math and check the discriminant before you  
call Math.sqrt 😉  
Otherwise, require ‘complex’ 😉

Jano

---

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/5 "2008-02-25T16:15:44Z")

</div>

Sebastian H. wrote:

> HTH,  
> Sebastian

Thanks all for your replies. The input was invalid. I need to check for  
legal input.  
But I am new and just learning. Still trying to figure out whats going  
on here.  
The docs are hard to understand. So I am programming very basically.

Thanks so much  
Chris

---

<div class="post-metadata">

**Author:** ![Christian\_Rubio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8dc957/32.png) [@Christian\_Rubio](https://www.ruby-forum.com/u/Christian_Rubio)\
**Post date:** [February 25, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/newbie-question/130115/6 "2008-02-25T16:05:07Z")

</div>

Christian Rubio wrote:

> quadratic.rb:17:in `sqrt': Numerical argument out of domain - sqrt (Errno::EDOM) from quadratic.rb:17:in`findroots’  
> from quadratic.rb:30

This means that your argument to sqrt is negative.  
What values for b and c did you supply?

HTH,  
Sebastian
