# Im new... so can i have some help

**URL:** <https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996>\
**Category:** Ruby\
**Created:** [August 15, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996 "2008-08-15T23:06:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 15, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/1 "2008-08-15T23:06:31Z")

</div>

im new to ruby… and i need some help!  
im trying to make a quick app that takes an array of #'s  
puts them into a specific order ( i thik ill use the method “list” for  
that?"  
and gets the mean, median and mode of the array of numbers?  
how would i get what the user enters and puts it in  
this is what i got so far

array = {}  
num = gets.to\_i  
while num != 000

\*please note the “while num != 000” is for when the user enters 000, the  
program knows when to stop adding numbers to the array, and to get the  
mean median and mode

than u

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 15, 2008, 11:55pm UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/2 "2008-08-15T23:55:06Z")

</div>

On Fri, Aug 15, 2008 at 3:03 PM, Daniel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> array = {}
> 
> ## than u
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Is this a homework assignment? while num != 000 is not going to do what  
you  
expect. It will catch 0, 00, 000, 0000, 00000… as to\_i converts those  
to  
0.

also you would want to write it as

while num != 000  
num = gets.to\_i  
array \<\< num  
end

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 15, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/3 "2008-08-15T23:52:11Z")

</div>

Dear Daniel.

> im new to ruby… and i need some help!

welcome to Ruby. You’ll find help on this list 🙂

> im trying to make a quick app that takes an array of #'s  
> puts them into a specific order ( i thik ill use the method “list” for  
> that?"

Use sort for that (  
[class Array - RDoc Documentation](http://www.ruby-doc.org/core/classes/Array.html#M002208))

> and gets the mean, median and mode of the array of numbers?

There’s Array#inject for summing, median is the middle of the sorted  
Array,  
and modes are elements that are equal to Array#max.

I’d suggest you go through Chris P.'s little tutorial for Ruby:

[http://pine.fm/LearnToProgram/](http://pine.fm/LearnToProgram/)

or, if you have some love for comics … and more time …

[http://poignantguide.net/ruby/](http://poignantguide.net/ruby/)

to get an impression of the language.  
Also, there are many quizzes for Ruby with different solutions to a  
problem:

[http://rubyquiz.com/](http://rubyquiz.com/)

And of course, many excellent books, and readily written software  
packages (in your case,  
search e.g., for the statistics2 package on the RAA (Ruby Application  
Archive).

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 12:21am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/4 "2008-08-16T00:21:15Z")

</div>

ok this is what i wrote so far

array = []  
num = gets.to\_i  
while num != 000  
num = gets.to\_i  
array \<\< num  
array.sort  
end

why wont if list out the numbers?

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 12:24am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/5 "2008-08-16T00:24:53Z")

</div>

Daniel K. wrote:

> ok this is what i wrote so far
> 
> array = []  
> num = gets.to\_i  
> while num != 000  
> num = gets.to\_i  
> array \<\< num  
> array.sort  
> end
> 
> why wont if list out the numbers?

nvm i relized i didnt add the “puts”  
but it comes out like this

2  
5  
6  
8  
9

how do i make it so its in one line?

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 12:31am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/6 "2008-08-16T00:31:56Z")

</div>

On Fri, Aug 15, 2008 at 4:21 PM, Daniel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > array.sort
> 
> ## how do i make it so its in one line?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

If you want the array data as a string (i.e. one line) you can use the  
join  
method:

puts array.join(’ ‘) would print the numbers in the array separated by  
spaces  
puts array.join(’,') would separate the elements with a comma

You can separate them with whatever you want, it can even be multiple  
characters.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 1:09am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/7 "2008-08-16T01:09:35Z")

</div>

ok, so here is my code

array = []  
num = gets.to\_i  
while num != 0  
num = gets.to\_i  
array \<\< num  
end  
puts array.sort.join(’,’)

works well so far

Now, how would i take the numbers in the array, and add them…

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 10:35am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/8 "2008-08-16T10:35:28Z")

</div>

Le Fri, 15 Aug 2008 18:06:38 -0500,  
Daniel K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) a Ã©crit :

> array = []  
> num = gets.to\_i  
> while num != 0  
> num = gets.to\_i  
> array \<\< num  
> end  
> puts array.sort.join(‘,’)  
> works well so far  
> Now, how would i take the numbers in the array, and add them…

puts array.inject {|sum,n| sum+n}

([module Enumerable - RDoc Documentation](http://www.ruby-doc.org/core/classes/Enumerable.html))

---

<div class="post-metadata">

**Author:** ![backz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9f8e36/32.png) [@backz](https://www.ruby-forum.com/u/backz)\
**Post date:** [August 16, 2008, 1:55am UTC](https://www.ruby-forum.com/t/im-new-so-can-i-have-some-help/145996/9 "2008-08-16T01:55:34Z")

</div>

Daniel K. wrote:

> ok this is what i wrote so far

In general…

- ‘ok’ is spelled ‘okay’ - it’s an African word, not an acronym

- a good way to learn Ruby interactively is this link here:

[http://tryruby.hobix.com/](http://tryruby.hobix.com/)

It might lead you thru the basics for arrays…
