Computer Science Canada

problem with def name inside class?

Author:  TokenHerbz [ Thu Dec 16, 2010 11:30 am ]
Post subject:  problem with def name inside class?

I Have NO idea why this wont work. Inside of a class im making, I'm trying to 'initizalize' the object when its created. I came across something VERY weird.

If i use the 'def init' or 'def other_names' it works FINE, but if i use 'def initialize' it doesn't work, Why? That doesn't make sense to me...

This little code does not work....
Ruby:

def initialize(x,y)
  @x = x
  @y = y
end


so then why does this code work???!? its the same thing minus the def name. ( i call it properly, tested with other def names also and they work)
Ruby:

def init(x,y)
  @x = x
  @y = y
end

Author:  DemonWasp [ Thu Dec 16, 2010 12:09 pm ]
Post subject:  RE:problem with def name inside class?

Not knowing Ruby, I can't be sure, but I suspect it has something to do with this: http://ruby.activeventure.com/usersguide/rg/objinitialization.html

It looks like initialize may be a reserved name that can only be called by object construction (new).

Author:  wtd [ Thu Dec 16, 2010 12:10 pm ]
Post subject:  RE:problem with def name inside class?

Post all of your code.

Author:  TokenHerbz [ Thu Dec 16, 2010 12:36 pm ]
Post subject:  Re: problem with def name inside class?

okay wtd, I warn you its like a mess of testing stuff... I cant even get this thing to work right even tho tests look promising...

But if you change the "init"s to "initialize" it don't work at all and errors.

Ruby:

CARDS_PER_DECK = 52
SUITS_OF_CARDS = 4
DECKS_PLAYED_WITH = 4
TOTAL_CARDS_USED = CARDS_PER_DECK * DECKS_PLAYED_WITH

class Card
  attr_accessor :@value, :@suit

  def init(value, suit)

    if value == 0
      @value = "A"
    elsif value == 10
      @value = "J"
    elsif value == 11
      @value = "Q"
    elsif value == 12
      @value = "K"
    else
      @value = value
    end

    @suit = case suit
    when 0
      "S"
    when 1
      "C"
    when 2
      "H"
    when 3
      "D"
    else
      "?"
    end
  end

  def printCard
    print @value, @suit
  end
end

card_temp = 0
card = Array.new(TOTAL_CARDS_USED, Card.new)
DECKS_PLAYED_WITH.times{
  SUITS_OF_CARDS.times{|suit|
    (CARDS_PER_DECK / SUITS_OF_CARDS).times{|value|
      print value," - ",suit, "-", card_temp
      puts
      card[card_temp].init(value,suit)
      card_temp += 1
    }
  }
}

TOTAL_CARDS_USED.times{|x|
  card[x].printCard
  puts
}


Side note and question to (im still working on it but ill post the question anyways since my codes up)

But when i put the variables suit/value/temp_card they all show correctly, and so when i try to assign the correct values (passing to the class to use in the array) and print to see after, its always showing all the cards as King dimonds...?

Author:  jcollins1991 [ Thu Dec 16, 2010 1:05 pm ]
Post subject:  Re: problem with def name inside class?

I'm not 100% sure, but it look's like you're initializing the array with a single class instance, so when you loop through to initialize them you're doing init on the same card over and over, in which case the last init step would probably be with value = 12 and suit = 4 giving a king of diamonds as a results. Try creating a new card object in the loop instead of as a parameter to Array.new

Author:  TokenHerbz [ Thu Dec 16, 2010 1:09 pm ]
Post subject:  RE:problem with def name inside class?

ohh that would make sense yes, I figured i was declaring an array of values of class references independently... like card = array 1 .. 52 of ^Card

code:

card = Array.new(TOTAL_CARDS_USED)
card[x]=Card.new
card[x].init(x,y)


that works.

Author:  jcollins1991 [ Thu Dec 16, 2010 1:30 pm ]
Post subject:  Re: problem with def name inside class?

You could also try

[syntax=""]
# here multiple copies are created
a = Array.new(2) { Hash.new }
a[0]['cat'] = 'feline'
a
[/syntax]

To initialize it all in one line (example from Array documentation on ruby-doc.org)

Author:  TokenHerbz [ Thu Dec 16, 2010 1:58 pm ]
Post subject:  RE:problem with def name inside class?

im pretty sure my way is less typing then the 52 card combinations... thx tho

Author:  jcollins1991 [ Thu Dec 16, 2010 2:19 pm ]
Post subject:  Re: problem with def name inside class?

You don't need to type that much...

Ruby:

cards = Array.new(TOTAL_CARDS_USED) { |index|
  overall_number = index % 52
  card_number = overall_number % 4
  suit_number = overall_number % 12
 
  card = Card.new()
  card.init(card_number, suit_number)
  card
}



Not exactly one line, but it's a bit simpler then creating an array than iterating separately. And if you write your own "new" function for your class you can reduce the last 3 lines to one problably (something like "Card.new(card_number, suit_number)").

And you really don't need to use TOTAL_CARDS_USED.times...

Ruby:
card.each { |c| c.printCard }


Also, you might want to consider writing a "to_s" function instead (standard for most built in classes), it's not really necessary here but it can be useful in other situations where you need to both manipulate and output string versions of your classes.

Author:  DemonWasp [ Thu Dec 16, 2010 2:48 pm ]
Post subject:  RE:problem with def name inside class?

You'll probably want suit_number = overall_number % 13, actually. Then, there's also the fact that you could just use index instead of overall_number.

Author:  TokenHerbz [ Thu Dec 16, 2010 2:55 pm ]
Post subject:  RE:problem with def name inside class?

hmm thats actually really freaking neat... i'll redo my code trying it this way.


: