Jump to content
dbchest

Hash Expressions

Recommended Posts

i don't fully understand the potential of hash arrays. i understand they act as a sort of road map for assigning values to symbols, etc. i would like to walk through some examples to get a better idea of how hash arrays work. below is a practical example of a system i am developing. i would appreciate if someone could help me understand how to pull the desired data:

  #--------------------------------------------------------------------------
  # * Constants (Graphics and Data Assignment)
  #--------------------------------------------------------------------------
  NOTESET = 'Noteset' # filename for graphics
  DATA    = {:DOWN  => ['Audio\SE\Cursor2', 0,  3],
             :LEFT  => ['Audio\SE\Cursor2', 1,  2],
             :RIGHT => ['Audio\SE\Cursor2', 2,  1],
             :UP    => ['Audio\SE\Cursor2', 3,  0],
             :A     => ['Audio\SE\Cursor2', 4,  2],
             :C     => ['Audio\SE\Cursor2', 6,  0]}
  #--------------------------------------------------------------------------
  # * Constants (Settings for Playback)
  #--------------------------------------------------------------------------

where:

[audio filename, index within spritesheet, line number for display]

 

now for the practical example:

  #--------------------------------------------------------------------------
  # * Draw All Notes
  #--------------------------------------------------------------------------
  def draw_all_notes
    @composition.each_index do |i|
      draw_note(WHAT CAN I PUT HERE?)
    end
  end
  #--------------------------------------------------------------------------
  # * Draw Note
  #--------------------------------------------------------------------------
  def draw_note(index, x, y)
    bitmap = Cache.system(NOTESET)
    rect = Rect.new(index % 6 * 24, index / 6 * 24, 24, 24)
    contents.blt(x, y, bitmap, rect)
  end

when defining my arguments, how can i pull the data from my hash array? i need to pull the spritesheet index and the line number.

Share this post


Link to post
Share on other sites

Well to work with a hash, you use keys and assign values to the keys.

 

# init hash
your_hash = {}

# assign something to a key
your_hash[:name] = "Hime"

# Access the value of the key
p your_hash[:name]
It's just a way to store your data.

 

I'm not sure what you actually want to know.

Edited by Tsukihime

Share this post


Link to post
Share on other sites

the console and you are life savers. don't lock this thread yet. i have a feeling i will have more questions about this, but i have a basic understanding.

Edited by dbchest

Share this post


Link to post
Share on other sites

To put it roughly it's just like an array but instead of numeric index you can have whatever you want for an index.

Share this post


Link to post
Share on other sites

yea, turns out the main problem i experienced was using the proper method call for evaluating each element of my array with a block.

 

when working with symbols, you need the index processed as is so it reads the symbol. <= each do

when working with integers, you need the index processed as an integer so it can pull the index. <= each_index

when using hash arrays, you assign values to symbols, so naturally you will need a way to reference both types of data when evaluating a block. <= each_with_index

 

i kept using each do and each_index. i kept getting errors until i remembered about each_with_index and referenced both forms of my data for my block.

Edited by dbchest

Share this post


Link to post
Share on other sites

For hashes you can go with

@hash.each{ |key, value|
  my stuff here}

I don't know if you can use #each (the simplest one) with only one 'block argument' though (key or value), never tested it but you have #each_key and #each_value for that matter.

Edited by Riff

Share this post


Link to post
Share on other sites

yeah, i didn't take a real good look at the hash class. that was my bad. i spent a minute going over the methods listed, then i also checked ruby-doc (regendo provided me the link in the past) and now i'm up and running with hashes. i won't say i entirely understand how to reference values from them, because i still have to guess, test, and revise occasionally to grab the info i want, but that's a short price to pay.

 

thanks everyone.

Share this post


Link to post
Share on other sites

yea, turns out the main problem i experienced was using the proper method call for evaluating each element of my array with a block.

 

when working with symbols, you need the index processed as is so it reads the symbol. <= each do

when working with integers, you need the index processed as an integer so it can pull the index. <= each_index

when using hash arrays, you assign values to symbols, so naturally you will need a way to reference both types of data when evaluating a block. <= each_with_index

 

i kept using each do and each_index. i kept getting errors until i remembered about each_with_index and referenced both forms of my data for my block.

That doesn't really make sense to me.

 

each_with_index is the same as each except you have the index of the current element

 

[2,3,4].each_with_index do |num, i|
  p [num, i]
end
Results in

 

[2, 0]
[3, 1]
[4, 2]

Share this post


Link to post
Share on other sites

you're exactly right regendo.

 

tsukihime:

i'm developing my own version of a script that allows the player to perform instrumentals. the way i worded things above sounded bad because it comes off like i'm implying that those methods work the way i described above in general, but we know that's not true. in reality, i wanted to imply that in order to get the icons to draw on the screen properly (for my script alone), i needed to be able to reference both pieces of information.

 

when writing the definition to draw the icons, i repeatedly tried using each and each_index. i didn't take into account that those methods only reference one of the two pieces of information from the array's elements, and at the time i wrote that post i guess i didn't fully understand what each, each_index, and each_with_index actually referenced.

 

i believe this is how those methods work.

ray = [this, is, right] << if this is my array...
ray.each do |element|
 p element
end

ray.each_index do |index|
 p index
end

ray.each_with_index do |element, index|
 p element
 p index
end

will result in (respectively):

this
is
right

0
1
2

[this, 0]
[is, 1]
[right, 2]

correct me if i'm wrong?

Edited by dbchest

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×