SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: method(hash)

  1. #1
    SitePoint Addict JNKlein's Avatar
    Join Date
    Sep 2004
    Location
    New York, NY
    Posts
    258
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    method(hash)

    Not sure I'm understanding how this works ....

    Code:
    def method(args)
        return args['param']
    end
    
    method(:param => "hello world") # doesn't return anything
    What am I doing wrong?

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Symbols are not the same as strings, so you should be doing this:

    Code:
     def method(args)
       return args[:param]
     end
    Also, it is often handy to write a method that converts all hash keys from strings to symbols or vice versa, to do so you would do something like this:

    Code:
    class Hash
      def keys_to_symbols
    	self.each do |key,value|
    	  next if key.is_a?(Symbol)
    	  self[key.to_sym] = value
    	  self.delete(key)
    	end
      end
    end
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xmitchx
    Symbols are not the same as strings, so you should be doing this:
    This point still confuses me sometimes too.

    Here's a link about this:
    http://onestepback.org/index.cgi/Tec...bleStrings.red
    Erh


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
  •