Hi, i'm new. plus one question

I've been forced to work on some php lately and found myself thinking: "man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started reading about ruby.

Here's my question: is there a common way to rotate a string or an array?

I thought i could do some combination of .pop and .unshift or something, but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?

-travis

i am also a ruby-newbie.
so i am not sure that i could answer enough to your question but...

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

because pop is an explicit manipulator, even if it is not followed by '!'.

i guess you simply can use idea.last instead of pop.

q2hdp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

this is a known phenomenon, named 'decay'.
"ruby" stands for "radioactively unstable block yielder".
you should refrain from giving a shock on it,
and keep it away from any other highly radioactive matter.

i am sure that your supercollider did make a kind of side effect on your
ruby and make its decay period get accelerated.
hence, the rearmost particle in the array has vanished.
it would be worth to try again after powering off your supercollider.

moreover, gravitational equilibration is also important.
ruby prefers flat place.
if you can see huge mountain on your left side and sea on your right
side, ruby won't run as you expect, since it would be drifting to the
mountain side.

be careful.

qssp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
  def rotate(steps = 1)
    steps = steps % self.length
    self[-steps..-1].concat self[0...-steps]
  end
end

class String
  include Rotate
end

class Array
  include Rotate
end

string = "I am a String"
array = string.split

10.times do | i |
  p string.rotate(i)
  p array.rotate(i)
end

bschroed@black:~/svn/projekte/ruby-things$ ruby rotate.rb
"I am a String"
["I", "am", "a", "String"]
"gI am a Strin"
["String", "I", "am", "a"]
"ngI am a Stri"
["a", "String", "I", "am"]
"ingI am a Str"
["am", "a", "String", "I"]
"ringI am a St"
["I", "am", "a", "String"]
"tringI am a S"
["String", "I", "am", "a"]
"StringI am a "
["a", "String", "I", "am"]
" StringI am a"
["am", "a", "String", "I"]
"a StringI am "
["I", "am", "a", "String"]
" a StringI am"
["String", "I", "am", "a"]

regards,

Brian

···

On 22/09/05, travis laduke <wrong@socal.rr.com> wrote:

I've been forced to work on some php lately and found myself
thinking: "man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started
reading about ruby.

Here's my question: is there a common way to rotate a string or an
array?

I thought i could do some combination of .pop and .unshift or
something, but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?

-travis

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Hi,

the code:

  idea = ["a","b","c","d"]
  idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

  idea = ["a","b","c","d"]
  idea.unshift( idea.pop )
  p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

Michael

"travis laduke" <wrong@socal.rr.com> schrieb im Newsbeitrag
news:7501E02B-A490-423F-B99B-8EF43D1CA11F@socal.rr.com...

···

I've been forced to work on some php lately and found myself thinking:
"man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started reading
about ruby.

Here's my question: is there a common way to rotate a string or an array?

I thought i could do some combination of .pop and .unshift or something,
but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?

-travis

like playing golf near the ocean?

···

On Sep 21, 2005, at 10:51 PM, SHIGETOMI, Takuhiko wrote:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?

this is a known phenomenon, named 'decay'.
"ruby" stands for "radioactively unstable block yielder".
you should refrain from giving a shock on it,
and keep it away from any other highly radioactive matter.

i am sure that your supercollider did make a kind of side effect on your
ruby and make its decay period get accelerated.
hence, the rearmost particle in the array has vanished.
it would be worth to try again after powering off your supercollider.

moreover, gravitational equilibration is also important.
ruby prefers flat place.
if you can see huge mountain on your left side and sea on your right
side, ruby won't run as you expect, since it would be drifting to the
mountain side.

be careful.

qssp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

Hi --

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
def rotate(steps = 1)

      returns self if length.zero? # :slight_smile:

   steps = steps % self.length
   self[-steps..-1].concat self[0...-steps]
end
end

David

···

On Thu, 22 Sep 2005, [ISO-8859-1] Brian Schröder wrote:

--
David A. Black
dblack@wobblini.net

Hi --

Hi,

the code:

idea = ["a","b","c","d"]
idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

idea = ["a","b","c","d"]
idea.unshift( idea.pop )
p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

I think Travis wanted to do that without changing the original array
-- something like:

   idea = %w{a b c d}
   new_idea = [idea[-1], *idea[0..-2]]

(Obviously not a full implementation, but just illustrating the
non-changingness.)

David

···

On Thu, 22 Sep 2005, Michael Ehehalt wrote:

Michael

"travis laduke" <wrong@socal.rr.com> schrieb im Newsbeitrag
news:7501E02B-A490-423F-B99B-8EF43D1CA11F@socal.rr.com...

I've been forced to work on some php lately and found myself thinking:
"man, this sucks. i wish it was more like supercollider"
then i remembered supercollider is ruby-influenced so i started reading
about ruby.

Here's my question: is there a common way to rotate a string or an array?

I thought i could do some combination of .pop and .unshift or something,
but i ran into this problem:

idea = ["a","b","c","d"]
x = idea.pop
puts idea

why is idea changed? how do i make it stay the same?
why is it different than:

x = idea.reverse

where idea is left alone and only x is the reversed array?

-travis

--
David A. Black
dblack@wobblini.net

As Travis wanted the original array unchanged, you'd have to insert a #dup or #clone somewhere, e.g.

idea = ["a","b","c","d"]
cp = idea.dup
cp.push( cp.shift )

etc.

Kind regards

    robert

···

Michael Ehehalt <michael.ehehalt@no-spam.fecher.de> wrote:

Hi,

the code:

idea = ["a","b","c","d"]
idea.push( idea.shift )

lets the array rotate to the left and returns ["b", "c", "d", "a"]

the code:

idea = ["a","b","c","d"]
idea.unshift( idea.pop )
p idea

lets rotate to the right and returns: ["d", "a", "b", "c"]

Is this what you mean?

you guys are magicians, thanks

i thought not being able to rotate was the only thing standing in my way of finding all the anagrams (permutations) of a string so i can enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.

oh, and how to i point ruby to a directory where i want to put all my modules and such?, or where should i put all my modules and such? i'm on an apple computer.

travis

···

On Sep 22, 2005, at 5:00 AM, Brian Schröder wrote:

On 22/09/05, travis laduke <wrong@socal.rr.com> wrote:

Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
  def rotate(steps = 1)
    steps = steps % self.length
    self[-steps..-1].concat self[0...-steps]
  end
end

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

like playing golf near the ocean?

you've got the point!

googling with 'gravimetric survey' will help you.

xnfp://void/3d/universe/milky-way-galaxy/orion-arm/sol-solar-system/
3rd-planet/fareast/jp/tky/shigetomi.takuhiko.5618

Yeah, thats O(n!) IIRC. What you can try is

#!/usr/bin/env ruby
my_hand = %w( a b c ) # Only an example, put the letters you actually have here
seed = 'br' # Something already on the board you have room to build off
             # Note, put only the part that would be in your word
             # eg. if the word is bread, going down and you want to start at the e and go to the right
             # use only the letter e as your seed

File.open('/usr/share/dict/words') do |dictionary|
         dictionary.grep(/^#{seed}(?:#{my_hand.join('|')})+/) do |word|
                 word.chomp!
                 puts word if (word.split(//) - (seed.split(//) + my_hand)).empty?
         end
end

Note that its very, very limited and not smart at all. It'll narrow down your choices somewhat, and may be helpful in a minor way, but its just a heuristic and a poor one at that, not an alogirithm and not all the answers it gives you will be right.

Seems like a good Ruby Quiz though, a scrabble bot.

···

On Sep 23, 2005, at 12:41 AM, travis laduke wrote:

On Sep 22, 2005, at 5:00 AM, Brian Schröder wrote:

On 22/09/05, travis laduke <wrong@socal.rr.com> wrote:

Hope this helps:

bschroed@black:~/svn/projekte/ruby-things$ cat rotate.rb
module Rotate
  def rotate(steps = 1)
    steps = steps % self.length
    self[-steps..-1].concat self[0...-steps]
  end
end

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

you guys are magicians, thanks

i thought not being able to rotate was the only thing standing in my way of finding all the anagrams (permutations) of a string so i can enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.

oh, and how to i point ruby to a directory where i want to put all my modules and such?, or where should i put all my modules and such? i'm on an apple computer.

travis

Permutations are not an efficient way to find anagrams, note. Something
like this is better (untested):

dictfile = 'sowpods.txt'
anags = {}
IO.foreach(dictfile) {|word|
  word.chomp!
  word.upcase!
  alpha = word.split(//).sort.join('')
  anags[alpha] ||= # set the value to if it doesn't exist
  anags[alpha] << word
}

puts "Enter rack"
rack = gets.chomp.upcase.sort
n = rack.length

# work through all subracks
max = 2**n - 1
max.downto(1) {|i|
  s = ""
  0.upto(n) {|j|
    s << rack[i] if i[j] == 1
  }

  puts anags[s]
}

martin

···

travis laduke <wrong@socal.rr.com> wrote:

i thought not being able to rotate was the only thing standing in my
way of finding all the anagrams (permutations) of a string so i can
enhance my chances of beating my girlfriend at online scrabble.
turns out its a lot harder than i first imagined.