Swap new number into existing array

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

Attachments:
http://www.ruby-forum.com/attachment/8949/numbers.rb

···

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

The problem is in the "run" method: every time you call the "string"
method in there, you are basically resetting the @string instance
variable to its starting value.

Maybe try experimenting with using the @string variable directly in the
run method (instead of via the "string" method as you do currently) and
you will have better results!

···

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

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/\.

two major problems — others have said you are resetting @string every time you call the string method. The other problem is your if-elsif struct — you input into the answer local variable, but you’re checking number instead.

···

On Nov 21, 2013, at 3:07 PM, Richard Jones <lists@ruby-forum.com> wrote:

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

Attachments:
http://www.ruby-forum.com/attachment/8949/numbers.rb

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

Try this, you were close. I would try test driving you application to help you make sure your program is doing what you want it to.
You can do that on small projects like this.

Here is a good example using Rspec to drive out a binary search tree implementation but you can use mini test as well.

numbers.rb (925 Bytes)

···

--
Todd Pickell
Software Developer

On November 22, 2013 at 2:52:34 PM, Tamara Temple (tamouse.lists@gmail.com) wrote:

On Nov 21, 2013, at 3:07 PM, Richard Jones <lists@ruby-forum.com> wrote:

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

Attachments:
http://www.ruby-forum.com/attachment/8949/numbers.rb

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

two major problems — others have said you are resetting @string every time you call the string method. The other problem is your if-elsif struct — you input into the answer local variable, but you’re checking number instead.

two major problems — others have said you are resetting @string every

time you call the string method. The other problem is your if-elsif struct
— you input into the answer local variable, but you’re checking number
instead.

Others have said that, too. (Modulo a typo with a missing equals sign.)
That's okay, though, I did use a lot of words; I shouldn't expect people to
look at _all_ of them...

···

On Nov 23, 2013 6:52 AM, "Tamara Temple" <tamouse.lists@gmail.com> wrote:

Sent from my phone, so excuse the typos.

Here is a slightly better version.

numbers.rb (821 Bytes)

···

--
Todd Pickell
Software Developer

On November 22, 2013 at 3:11:41 PM, Todd Pickell (myappleguy@gmail.com) wrote:

Try this, you were close. I would try test driving you application to help you make sure your program is doing what you want it to.
You can do that on small projects like this.

Here is a good example using Rspec to drive out a binary search tree implementation but you can use mini test as well.

--
Todd Pickell
Software Developer

On November 22, 2013 at 2:52:34 PM, Tamara Temple (tamouse.lists@gmail.com) wrote:

On Nov 21, 2013, at 3:07 PM, Richard Jones <lists@ruby-forum.com> wrote:

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

Attachments:
http://www.ruby-forum.com/attachment/8949/numbers.rb

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

two major problems — others have said you are resetting @string every time you call the string method. The other problem is your if-elsif struct — you input into the answer local variable, but you’re checking number instead.

- numbers.rb, 1.2 KB

Todd Pickell wrote in post #1128342:

Here is a slightly better version.

Actually, your second one has a zero-index issue, because you can input
numbers 1,2,3,4, but the array indices are 0,1,2,3. Did you try driving
your application to make sure your program is doing what you want it to?

···

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

Good catch, it does have an off by one issue. A good test would have caught that. Thank you.

numbers.rb (825 Bytes)

···

--
Todd Pickell
Software Developer

On November 22, 2013 at 3:45:47 PM, Matthew Kerwin (lists@ruby-forum.com) wrote:

Todd Pickell wrote in post #1128342:

Here is a slightly better version.

Actually, your second one has a zero-index issue, because you can input
numbers 1,2,3,4, but the array indices are 0,1,2,3. Did you try driving
your application to make sure your program is doing what you want it to?

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