Richard Jones wrote in post #1128219:
> Hi,
>
> I've created the attached program but can't get my random number to pass
> into my array in place of another item in the array, can anyone help?
>
> Many Thanks
> Richard

A couple of reasons, but here's a big one:

  string[0] = @new_number

... calls the `string' function which, in your code, creates and returns 
a new array, whose elements are [25,50,75,100].  Then it overrides the 
0th element (the 25) with whatever value @new_number has, which in this 
case is a random number up to 15.

  puts string

... calls the `string' function again, which creates and returns a new 
array whose elements are [25,50,75,100].  Then it `puts'es that new 
array.

The other big problem is: in your if-elsif tests, you're comparing `if 
number == x' instead of `if answer = x'.

Here's a pattern I propose as a general rule for OOP:

1. don't assign values in simple "getter" functions
   * i.e., your `number' and `string' functions should not include an 
assignment/equals-sign
2. don't do any interesting work from `initialize'
   * this is where you should set up the initial value of @number and 
@string, but you shouldn't call `run' from there
3. don't refer to @variables anywhere except the 
getter/setter/initialise functions
   * this is not necessarily a good rule, but it isn't a bad one either

Without doing anything clever, apart from fixing the condition in the 
`if' statements, and applying my three guidelines above, I get the 
attached script.  There are other things you could do to make it more 
"rubyish", but this is a start.

Attachments:
http://www.ruby-forum.com/attachment/8950/numbers2.rb


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