Methods in ruby code

Hi,

I want to use the shuffle method for array, but I want to modify it a
little bit.
Is there any website or source where I can see the shuffle method in
ruby code, because often in Tutorials they show some of these methods in
Ruby Code and how they look in the libary.
I am looking forward to hear from you.

Yours sincerely
Greeneco

On Thu, Oct 10, 2013 at 7:04 PM, Green E. [email protected] wrote:

I want to use the shuffle method for array, but I want to modify it a
little bit.

I would not change the library method but rather implement another
shuffle.

What modifications do you want to do?

Is there any website or source where I can see the shuffle method in
ruby code, because often in Tutorials they show some of these methods in
Ruby Code and how they look in the libary.
I am looking forward to hear from you.

You can try to read the C source - it’s usually not too hard to
understand.

Kind regards

robert

Hi,
thanks for your quick answer.
I want to use shuffle but I want it to take parameter and act the same
if I give it the same parameter.
e.g
[1,2,3].shuffle!(5) #=> [2,1,3]
and when I do this again
[1,2,3].shuffle!(5) #=> [2,1,3]
and when I use a different number
[1,2,3].shuffle!(4) #=> [1,2,3]
there should be a different result

I want to do something like that.

King regards
Greeneco

Yes you are right it would not be shuffle anymore.
And thank you for your two solutions, but the second one has less
sideeffects but I cannot just use this if I do not close the programm
without saving, because then it would be lost every time I restart. The
first has side effects but I guess it is that what I am looking for.

But what do you think of:
ary = [“1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”]
number = 9
ary_zahlen.shuffle!(random: Random.new(number))

Yours sincerely
Greeneco

On Thu, Oct 10, 2013 at 7:22 PM, Green E. [email protected] wrote:

there should be a different result
But then it’s not shuffle any more, is it? You basically associate a
particular order with a number as key. I can think of two approaches:

  1. manipulate the random generator via the seed:

irb(main):001:0> a = (1…5).to_a
=> [1, 2, 3, 4, 5]
irb(main):002:0> srand 0
=> 38759114252116045413516501677716616995
irb(main):003:0> a.shuffle
=> [1, 4, 2, 5, 3]
irb(main):004:0> srand 0
=> 0
irb(main):005:0> a.shuffle
=> [1, 4, 2, 5, 3]
irb(main):006:0> srand 4
=> 0
irb(main):007:0> a.shuffle
=> [1, 2, 4, 3, 5]
irb(main):008:0> srand 4
=> 4
irb(main):009:0> a.shuffle
=> [1, 2, 4, 3, 5]

  1. use a Hash to store the fixed relation:

irb(main):011:0> a = (1…5).to_a
=> [1, 2, 3, 4, 5]
irb(main):012:0> cache = Hash.new {|h,k| h[k] = a.shuffle}
=> {}
irb(main):013:0> cache[0]
=> [5, 3, 1, 4, 2]
irb(main):014:0> cache[0]
=> [5, 3, 1, 4, 2]
irb(main):015:0> cache[5]
=> [5, 2, 3, 1, 4]
irb(main):016:0> cache[5]
=> [5, 2, 3, 1, 4]
irb(main):017:0> cache[5]
=> [5, 2, 3, 1, 4]
irb(main):018:0> cache[2]
=> [5, 4, 2, 1, 3]
irb(main):019:0> cache[2]
=> [5, 4, 2, 1, 3]
irb(main):020:0> cache
=> {0=>[5, 3, 1, 4, 2], 5=>[5, 2, 3, 1, 4], 2=>[5, 4, 2, 1, 3]}

I’d prefer approach 2 because that does not have side effects on other
parts of the program which want to generate random numbers.

Kind regards

robert

Am 10.10.2013 19:04, schrieb Green E.:

I want to use the shuffle method for array, but I want to modify it a
little bit.
Is there any website or source where I can see the shuffle method in
ruby code, because often in Tutorials they show some of these methods in
Ruby Code and how they look in the libary.
I am looking forward to hear from you.

There is a GitHub mirror of the source code:

GitHub - ruby/ruby: The Ruby Programming Language

Array#shuffle is not implemented in Ruby but in C,
you can find it here:

https://github.com/ruby/ruby/blob/trunk/array.c (line 4381)

Regards,
Marcus

Am 10.10.2013 19:45, schrieb Robert K.:

[1,2,3].shuffle!(4) #=> [1,2,3]
=> 38759114252116045413516501677716616995
irb(main):008:0> srand 4
irb(main):013:0> cache[0]
=> [5, 4, 2, 1, 3]
robert
Regarding side effects:

Looking at the source code, I just noticed you can pass a random
generator to shuffle. From the ri documentation:

shuffle → new_ary
shuffle(random: rng) → new_ary

Returns a new array with elements of self shuffled.

a = [ 1, 2, 3 ] #=> [1, 2, 3]
a.shuffle #=> [2, 3, 1]

The optional rng argument will be used as the random number generator.

a.shuffle(random: Random.new(1)) #=> [1, 3, 2]

It seems to be there since 1.9.3!
This would solve the side effect problem.

Regards,
Marcus

On Thu, Oct 10, 2013 at 9:16 PM, [email protected] wrote:

Am 10.10.2013 19:45, schrieb Robert K.:

a = [ 1, 2, 3 ] #=> [1, 2, 3]
a.shuffle #=> [2, 3, 1]

The optional rng argument will be used as the random number generator.

a.shuffle(random: Random.new(1)) #=> [1, 3, 2]

It seems to be there since 1.9.3!
This would solve the side effect problem.

I had tried that (actually it was my first attempt) but to no avail:

irb(main):001:0> (1…10).to_a.shuffle nil
ArgumentError: wrong number of arguments (1 for 0)
from (irb):1:in shuffle' from (irb):1 from /usr/bin/irb:12:in
irb(main):002:0> (1…10).to_a.shuffle Random.new
ArgumentError: wrong number of arguments (1 for 0)
from (irb):2:in shuffle' from (irb):2 from /usr/bin/irb:12:in
irb(main):003:0> RUBY_VERSION
=> “1.9.3”

Kind regards

robert

Excerpts from Robert K.'s message of 2013-10-11 13:13:42 +0200:

This would solve the side effect problem.
from (irb):2:in shuffle' from (irb):2 from /usr/bin/irb:12:in
irb(main):003:0> RUBY_VERSION
=> “1.9.3”

The random number generator must be passed as a (pseudo) keyword
argument, that is as a hash, either using the new ruby 1.9 syntax, as
shown by the ri documentation:

a.shuffle random: Random.new

or using the standard hash syntax:

a.shuffle :random => Random.new

Stefano

On Fri, Oct 11, 2013 at 2:29 PM, Stefano C.
[email protected] wrote:

Excerpts from Robert K.'s message of 2013-10-11 13:13:42 +0200:

On Thu, Oct 10, 2013 at 9:16 PM, [email protected] wrote:

irb(main):002:0> (1…10).to_a.shuffle Random.new
shown by the ri documentation:
Ah! Thanks for the education! I had myself mislead by the
ArgumentError (above). I should have checked #arity

irb(main):001:0> Array.instance_method(:shuffle).arity
=> -1

… or the docs. :wink:

Kind regards

robert

Hi,

thank you for your big help, you really helped me out. :slight_smile:

King regards
Green E.