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

···

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

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

···

On Thu, Oct 10, 2013 at 7:04 PM, Green Eco <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

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

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.

Yours sincerely
Greeneco

···

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

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:04, schrieb Green Eco:

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.

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

Hi,

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

King regards
Green Eco

···

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

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]

2. 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

···

On Thu, Oct 10, 2013 at 7:22 PM, Green Eco <lists@ruby-forum.com> wrote:

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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

···

Am 10.10.2013 19:45, schrieb Robert Klemme:

On Thu, Oct 10, 2013 at 7:22 PM, Green Eco <lists@ruby-forum.com> wrote:

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

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]

2. 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

--
GitHub: https://github.com/stomar/
PGP: 0x6B3A101A

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 `<main>'
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 `<main>'
irb(main):003:0> RUBY_VERSION
=> "1.9.3"

Kind regards

robert

···

On Thu, Oct 10, 2013 at 9:16 PM, <sto.mar@web.de> wrote:

Am 10.10.2013 19:45, schrieb Robert Klemme:

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.

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

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

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

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 `<main>'
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 `<main>'
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 Thu, Oct 10, 2013 at 9:16 PM, <sto.mar@web.de> wrote:
> Am 10.10.2013 19:45, schrieb Robert Klemme:

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

···

On Fri, Oct 11, 2013 at 2:29 PM, Stefano Crocco <stefano.crocco@alice.it> wrote:

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

On Thu, Oct 10, 2013 at 9:16 PM, <sto.mar@web.de> wrote:

> 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 `<main>'
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 `<main>'
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:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/