Forum: Ruby on Rails Help - Hash.to_params

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f7716186bac5a8a2dc8007b70928c06?d=identicon&s=25 Luke Ivers (technodolt)
on 2007-02-08 20:37
This is my current implementation for Hash.to_params

If you have spare time, please help me optimize this.

Thanks.

class Hash
  def to_params(parent = '')
    ret = ''
    self.keys.each do |key|
      if self[key].is_a? Hash
        if parent == ''
          ret += self[key].to_uri(key.to_s)
        else
          ret += self[key].to_uri(parent + "[#{key.to_s}]")
        end
      else
        if parent == ''
          ret += "#{key}=#{self[key]}&"
        else
          ret += "#{parent}[#{key}]=#{self[key]}&"
        end
      end
    end
    return ret.chomp('&')
  end
end
72eb65fa089082d3bd8b38a5596ad331?d=identicon&s=25 Keynan Pratt (keynan)
on 2007-02-08 20:49
what do you plan to use this for?

as it would seem url_for() would do just fine
0f7716186bac5a8a2dc8007b70928c06?d=identicon&s=25 Luke Ivers (technodolt)
on 2007-02-08 22:27
Keynan Pratt wrote:
> what do you plan to use this for?
>
> as it would seem url_for() would do just fine

How do I test url_for in script/console?  I'm not exactly certain how it
works, and I'd like to test and see if it does what I want.

Thanks.
This topic is locked and can not be replied to.