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

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!

Richard J. 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 itputs’es that new
array.

The other big problem is: in your if-elsif tests, you’re comparing if number == x' instead ofif 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' andstring’ 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.

On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:


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 youre checking
number instead.

On Nov 23, 2013 6:52 AM, “Tamara T.” [email protected]
wrote:

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 youre 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…

Sent from my phone, so excuse the typos.

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.
GitHub - tapickell/tree_of_bin

Todd Pickell
Software Developer

On November 22, 2013 at 2:52:34 PM, Tamara T.
([email protected]) wrote:

On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:


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.

Here is a slightly better version.

Todd Pickell
Software Developer

On November 22, 2013 at 3:11:41 PM, Todd Pickell ([email protected])
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.
GitHub - tapickell/tree_of_bin

Todd Pickell
Software Developer

On November 22, 2013 at 2:52:34 PM, Tamara T.
([email protected]) wrote:

On Nov 21, 2013, at 3:07 PM, Richard J. [email protected] wrote:


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?

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

Todd Pickell
Software Developer

On November 22, 2013 at 3:45:47 PM, Matthew K.
([email protected]) 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/.