Forum: Ruby swap new number into existing array

06cddcc39ebb4e2eb76db3072067cf4b?d=identicon&s=25 Richard Jones (richyjones)
on 2013-11-22 00:07
Attachment: numbers.rb (801 Bytes)
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
Df9ff54e701e96e9b83b0b71254f2e40?d=identicon&s=25 Michael Bostler (obiruby)
on 2013-11-22 00:49
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!
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-11-22 01:04
Attachment: numbers2.rb (837 Bytes)
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.
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-22 21:52
(Received via mailing list)
On Nov 21, 2013, at 3:07 PM, Richard Jones <lists@ruby-forum.com> 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.
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-11-22 22:01
(Received via mailing list)
On Nov 23, 2013 6:52 AM, "Tamara Temple" <tamouse.lists@gmail.com>
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.
4138c636795abe4a88333591a3fa5519?d=identicon&s=25 Todd Pickell (Guest)
on 2013-11-22 22:12
(Received via mailing list)
Attachment: numbers.rb (925 Bytes)
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.
https://github.com/myappleguy/tree_of_bin
-- 
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:

>
>
> --
> 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.
4138c636795abe4a88333591a3fa5519?d=identicon&s=25 Todd Pickell (Guest)
on 2013-11-22 22:30
(Received via mailing list)
Attachment: numbers.rb (821 Bytes)
Here is a slightly better version.
-- 
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.
https://github.com/myappleguy/tree_of_bin
-- 
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:

>
>
> --
> 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
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2013-11-22 22:45
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?
4138c636795abe4a88333591a3fa5519?d=identicon&s=25 Todd Pickell (Guest)
on 2013-11-23 16:07
(Received via mailing list)
Attachment: numbers.rb (825 Bytes)
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 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/.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.