SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Trying to get symbols that are stored in a hash into an array, help needed.

    Hey guys I'm pretty new to Ruby and programming in general. I'm reading the Ruby book "Beginning Ruby - From Novice To Professional" and I'm doing one of the included projects - building a basic text adventure.

    I'm done with the text adventure but now I want to expand the features with some of my own methods but I'm running into problems.

    What I want to do is to have a method that prints out in which directions (south, north, east, west), from any given room, there are exists to other rooms.

    To do this I want to iterate through a hash with the each method and store every key (not value) in an array so that I then can see how many exits there are to the room where the player is at and print out a message of those exits. The keys are symbols.

    So I've come this far:
    Code Ruby:
    def find_exits
      find_room_in_dungeon(@player.location).connections.each {|direction, location| }
    end

    This part
    Code Ruby:
     find_room_in_dungeon(@player.location)
    finds the current room object where the player is at.

    This is the Room class:
    Code Ruby:
    class Room
     
          #Creates the objects variables that store infromation about the Room.
          attr_accessor :reference, :name, :description, :connections
     
          #Sets the information that is set when the object is created to the object variables for storage.
          def initialize(reference, name, description, connections)
             @reference = reference
             @name = name
             @description = description
             @connections = connections
          end
     
          #Defines a method that returns the full description of the Room object. 
          def full_description
           @name + "\n\nYou are in " + @description
          end
       end

    @connections is a object variable that stores the hash that contains directions and locations like so for example: {:east => :large_room}

    So I managed to store the directions of any given Room object with this line:
    Code Ruby:
     find_room_in_dungeon(@player.location).connections.each {|direction, location| }
    but how do I get what's in direction into an array?

    I have no idea how to do this. Any help is appreciated.

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do it like this:

    Code Ruby:
    directions = []
     
    find_room_in_dungeon(@player.location).connections.each do |direction, location|
      directions << direction
    end

    But this is better:

    Code Ruby:
    directions = find_room_in_dungeon(@player.location).connections.keys

    Hash#keys returns all keys present in the hash.

  3. #3
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot I got it working! That's exactly what I needed.

    And btw it is okay to ask general Ruby questions here, I mean it's not only a RoR forum, right?

  4. #4
    SitePoint Member
    Join Date
    Jun 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so I need some more help if that's ok.
    This is how it looks so far:

    Code Ruby:
     def find_exits
          directions = find_room_in_dungeon(@player.location).connections.keys
     
          if directions.length == 1
           puts "You see an exit to your " + directions[0].to_s
          elsif directions.length == 2
           puts "You see exits to your " + directions[0].to_s + " and to your " + directions[1].to_s
          else
             print "You see exits to your"
             directions.each {|direction| print " " + direction.to_s + ","} 
          end
     
       end

    It works good when the room has one or two exits for example:
    Code Ruby:
    d.add_room(:large_room, "Large room", "a large, empty room used for unknown purposes", {:west => :hallway})

    produces this when find_exits is run: "You see an exit to your west"

    Now the problem is when there's more then 2 directions. As you can see in the code when there's more then 2 objects in the directions array this is executed:

    Code Ruby:
    print "You see exits to your"
    directions.each {|direction| print " " + direction.to_s + ","}

    This isn't ideal beacuse it just prints the directions with a comma afterwards. On one room object I have the output is this: "You see exits to your west, north, eats,"

    I want it to print "You see exits to your west, north and east" but I can't figure out how to do that unless I code one separate elsif for every possible number of directions, and that's not a good way.
    Is there some way I can get that output and still use an interator?

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd do it this way:

    Code Ruby:
    def array_in_words(array)
      case array.length
      when 1: array.first.to_s
      when 2: "#{array[0]} and #{array[1]}"
      else array[0..-3].join(", ") + ", #{array[-2]} and #{array[-1]}"
      end
    end
     
    array_in_words(["one"]) # => "one"
    array_in_words(["one", "two"]) # => "one and two"
    array_in_words(["one", "two", "three"]) # => "one, two and three"

  6. #6
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Tical View Post
    And btw it is okay to ask general Ruby questions here, I mean it's not only a RoR forum, right?
    You are correct, ask away!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •