Do not understand the error messages

<meta http-equiv="content-type" content="text/html; 

charset=ISO-8859-1">

Hello,

I have this :

def random_select(array, n)
  numbers = n.times.map{ Random.rand(array.length) }
  numbers.each do |x|
      puts array[x]
  end
end


but I see these error messages:

Makes a call to 'rand' or 'Array#shuffle'
RSpec::Expectations::ExpectationNotMetError
expected false to be true
returns only the values present in the array passed to the method
RSpec::Expectations::ExpectationNotMetError
expected [902, 467, 807, 569] to include 0

Can someone explain both to me ?

Roelof


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

These are RSpec issues. Something in your test suite is not meeting
expectations.

On 6/21/2014 09:01, Roelof W. wrote:

Roelof


Rylee F.
[email protected]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (MingW32)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBCgAGBQJTpZEIAAoJEAWmNCH2N+MzEOgH/is5GqlIsZKME+WFShQQeSSU
rx/f8hmd/TtIbPRBDZPQCTBsuYhl/Rqc4m1gnZE4rN+43OrIiYc3/vQtnylADJ9i
mZDgX/5Mpr/cYqf9uAGG0JWwnYYuO3wl0THXrI4V6Qs2hmiAC6CWIDgipRRlxGdt
UUqhnvQ4Gt6HO9laOfo3iHQhNydDhwSIa2B4eXhvRwtL3aI/ouw+80G65QKxKFr4
/mvcaU/3imRhPs42q2F6Jxe2QmJhmYHOUyGmiisaJIdhv0dqYEE6J3XAEeaasARA
ovdgzpUyZUcGXMY9qMEUU5F5NeMwlH/gjozWB440lbSO0U3nubjbG9EYw0czfh8=
=WPM1
-----END PGP SIGNATURE-----

I know that and there are not mine expectations but of the site
rubymonkey

Roelof

Rylee F. schreef op 21-6-2014 16:04:

On Jun 21, 2014, at 12:14 PM, Roelof W. [email protected] wrote:

RSpec::Expectations::ExpectationNotMetError expected [902, 467,

Roelof

I am not familiar with rubymonkey, but the (perhaps poorly worded?)
error messages suggest that they expected you to call a different rand.
There is a rand method in Kernel, which is the one which you usually get
as Kernel is an ancestor of most objects. For example in irb (an
interactive ruby shell):

~/tmp ∙ irb
irb(main):001:0> class X
irb(main):002:1> def try_rand
irb(main):003:2> rand(10)
irb(main):004:2> end
irb(main):005:1> end
=> :try_rand
irb(main):006:0> X.new.try_rand
=> 2
irb(main):007:0> X.ancestors
=> [X, Object, Kernel, BasicObject]
irb(main):008:0> Kernel.rand(10)
=> 3

It also looks like they expect your random select to return items from
the array, and your method returns a list of array indices, you probably
picked index 0 at random, and although you print out array[x] the each
method on array returns the original array. For example in irb:

~/tmp ∙ irb
irb(main):001:0> array = [902, 467, 807, 569]
=> [902, 467, 807, 569]
irb(main):002:0> random_indexes = [ 0, 3 ]
=> [0, 3]
irb(main):003:0> random_indexes.each do |x|
irb(main):004:1* puts array[x]
irb(main):005:1> end
902
569
=> [0, 3]

And I guess the site was checking to see if 0 (one of the indexes you
returned) was in the original array.

Often the error messages guide you to a solution, for example they
suggest that they expected shuffle or rand to be called, so I would
probably have relied on Ruby’s simplicity and seen what Array#shuffle
does, and how to get the first “n” elements of an Array -
http://www.ruby-doc.org/core-2.1.2/Array.html is a good place to start.

Hope this helps,

Mike

Hello,
Makes a call to ‘rand’ or ‘Array#shuffle’

UUqhnvQ4Gt6HO9laOfo3iHQhNydDhwSIa2B4eXhvRwtL3aI/ouw+80G65QKxKFr4
/mvcaU/3imRhPs42q2F6Jxe2QmJhmYHOUyGmiisaJIdhv0dqYEE6J3XAEeaasARA
ovdgzpUyZUcGXMY9qMEUU5F5NeMwlH/gjozWB440lbSO0U3nubjbG9EYw0czfh8=
=WPM1
-----END PGP SIGNATURE-----

Mike S. [email protected]
http://www.stok.ca/~mike/

The “`Stok’ disclaimers” apply.

I do not understand them on this way :

Makes a call to ‘rand’ or ‘Array#shuffle’
RSpec::Expectations::ExpectationNotMetError expected false to be
true

I do use rand. See here : numbers = n.times.map{
Random.rand(array.length) }

returns only the values present in the array passed to the method
RSpec::Expectations::ExpectationNotMetError expected [902, 467,
807, 569] to include 0

I do that. I looked how many numbers there are. choose 2 numbers of it
by thier index.
and why schould it include 0 in a array which not contains 0.

Roelof

Roelof W. schreef op 21-6-2014 16:10:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs