Struct Classes ??

● ARCHIVED · READ-ONLY
Started by ?????? 10 posts View original ↗
  1. So recently I've been playing around with some structs.. Figured I could do this...

    class Person < Struct.new:)name,:dob,:stats) def age Time.now.year - split_dob.last end def birthday? Time.now.mon == split_dob[1] && Time.now.mday == split_dob[0] end def split_dob @split_dob ||= self[:dob].split("/").map {|i| i = i.to_i } end def mhp self[:stats][0] end def mmp self[:stats][1] end enddekarg = [[100, 50], '13/11/1989', 'Dekita']dekita = Person.new *dekarg.compact.reversedekita.each_pair do |pair| puts %Q[#{pair} = #{dekita.send(pair)}]endputs ""puts "bday? = #{dekita.birthday?}"puts "age = #{dekita.age}"puts "mhp = #{dekita.mhp}"puts "mmp = #{dekita.mmp}"So yea... I thought that was pretty cool...

    Anyone else got cool things you can do with structs? I would love to learn about them ^_^
  2. You do realize that structs can have their own methods, yes? Doing what you are rather defeats the purpose of using a Struct. Just do this:

    Person = Struct.new:)name,:dob,:stats) do  def age    Time.now.year - split_dob.last  end  def birthday?    Time.now.mon == split_dob[1] && Time.now.mday == split_dob[0]  end  def split_dob    @split_dob ||= self[:dob].split("/").map {|i| i = i.to_i }  end  def mhp    self[:stats][0]  end  def mmp    self[:stats][1]  endenddekarg = [[100, 50], '13/11/1989', 'Dekita']dekita = Person.new *dekarg.compact.reversedekita.each_pair do |pair|  puts %Q[#{pair} = #{dekita.send(pair)}]endputs ""puts "bday? = #{dekita.birthday?}"puts "age = #{dekita.age}"puts "mhp = #{dekita.mhp}"puts "mmp = #{dekita.mmp}"Edit: fixed the formatting that the site decided to eat.
  3. lol yes I do. I just thought having it as a class could also have benefits somewhere along the lines.

    And I've been having some formatting problems today as well. Perhaps an issue with the site :/
  4. Having it as a class like that actually only causes problems--it means that the Struct you created is an extra anonymous class that's just floating around, but never used. If you want a class, just define a class. All that Struct does is create a subclass that contains a constructor and some accessors. It's just meant to be a lightweight container for data, not a substitute for actual classes.
  5. ok yea, that makes sense :)
  6. I frequently use structs for configuration data.

    Makes for much nicer flat configuration code compared ugly nested hash and array trees.

    *hugs*

     - Zeriab
  7. I assume you mean when dumped/compressed? Good to know :)
  8. No, no... I mean when working with them normally when scripting. It's nicer having mouse_pos.x and mouse_pos.y than mouse_pos[0] and mouse_pos[1].

    I mention configuration data as I usually treat them as simple structured data. Whether it's a class or a structure depends on the situation.

    *hugs*
  9. ahh ok I get what you mean now.

    I would agree that yea, that would make for a much nicer code. Guess it just depends on personal preference there though.. Like, dont think any real difference in performance would be gained either way (using an array or hash or struct) - unless you gotta iterate, at which point the array or struct would probably be superior. :D
  10. I never used structs outside of warcraft 3... I actually didn't know before that they exist in RGSS3 too. Anyway, to me it seems like they doesn't do much difference. It seems like things that they do can also be done using modules for example.