Confusion with Hashes and Arrays when using steam-condenser Ruby gem

147 views Asked by At

I'm trying to use the steam-condenser gem to pull a list of games for a particular user (myself). So far I have the following:

require 'steam-condenser'

id = SteamId.new 'tamachan87'
games_owned = id.games

games_owned is now a hash, containing keys and arrays.

If I call games_owned.values in IRB I will get a result that contains all the information of those games, from it's ID number to the name to its logo hash.

However, when I use the following:

games_owned.each do |key, array|
    puts "==== #{key} ===="
    puts array
end

I get just the first value of the array such as:

==== 200260 ====
#<SteamGame:0x00000100beb0a8>

Each value/array thing has an @name variable which is the only thing I want to pull.

Could someone please help me to better understand these hashes and how I can pull specific data (@name) from them?

Thanks in advance.

3

There are 3 answers

0
Koraktor On BEST ANSWER

The return value of SteamId#games is not a hash of arrays, it's a hash of SteamGame objects.

Your example code could be written like that:

games_owned.each do |app_id, game|
  puts "==== #{app_id} ===="
  puts game.name
end

See the documentation of SteamId for more information.

0
bkdir On

If you want to print your Steamgame object in a pretty way by using "puts" you should overwrite "to_s" in your Steamgame class.

The reason you are getting different results is puts uses to_s method to print its argument (which is steamgame object itself in your case with specific id). If you use "p" or "puts array.inspect" instead of "puts", it will debug your object and you will see irb like results.

If you have an attribute like @name in your Steamgame class, you should be able to get it just by saying: games_owned.values[i].name or puts array.name. did you try that?

0
Alex.Bullard On

The reason you get an unfriendly class name and memory value is that the SteamGame class does not define an inspect method. However if all you're interested is printing the names of the games, use puts key.name