0

Please, do not attack. Here's the example and code that made me think about this.

I'm studying ruby on rails and am in the midst of the MVC and params[] operator. And have been laying down some proofs or something, reminders on the inside cover of my notebook dedicated to rails, anyway

"So we don't just want to call tweet #1. Tweet.find(1) isn't enough."
===> we need Tweet.find(params[:id])

params[:id] aka params = { :id => 1 }

So I was labeling the different parts to my self, starting with the hash. "The 1 is the value, the :id is the key, the "params" is the....pointer?

Is params a pointer? Is it a variable?

EDIT: Are there any other "variables" (for a lack of a better term) that accept :keys without values other than params? Is this a special type of "container" or is it normal to just accept :keys without values EDIT2: Are all variables technically pointers? If this true, are :keys inside of hashes technically pointers as well? Haha if this is true params is a pointer pointing to a pointer, which sounds about right.

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100

3 Answers3

3

I don't think you're using the right terms here. Maybe this will help:

  • The params token is a variable name that holds a reference to some object.
  • Technically, params is a method that returns a reference to a Hash object.
  • In practice, you treat params as a hash because params.is_a?(Hash) == true.
  • The :id token is a Symbol object.
  • In params[:id] the :id is the key for a hash value.

IMHO, it's generally not useful to think of objects in Ruby as pointers--unless you're deliberately using them that way for some reason. But yes, under the hood there are a lot of layers of indirection that turn C pointers into Ruby objects at runtime.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 1
    I'm not sure what you mean by saing it's a hash of hashes but i think you are wrong. It's just an hash, it's values are any kind of objects, strings, fixnum, arrays or hashes. – Ismael Abreu Jun 08 '12 at 01:25
  • `params` in a typical rails app always contains non-hash values, namely `controller` and `action`. Your statement is misleading, IMHO. – Sergio Tulentsev Jun 08 '12 at 01:43
  • `params` is actually a method that returns a reference to a `HashWithIndifferentAccess` (which subclasses `Hash`). – mu is too short Jun 08 '12 at 01:54
  • @muistooshort Absolutely. I *think* the argument is about how it's typically used, not how it's constructed. I've gone ahead and given the hash-of-hashes tag a wiki entry, so we can all at least be arguing about the same thing. :) – Todd A. Jacobs Jun 08 '12 at 02:01
  • 1
    Shouldn't that be a *method-that-returns-a-hash-of-hashes* tag? Then we'd be arguing about the same thing :) – mu is too short Jun 08 '12 at 02:04
  • @CodeGnome: I still object this statement. hash-of-hashes is just one small part of everyday's work: handling posted forms. – Sergio Tulentsev Jun 08 '12 at 02:05
  • I was also going to mention the "hash-of-hashes" thing--IMO it's worth mentioning that it always has top-level entries, and won't necessarily contain embedded hashes, under many circumstances. It's a minor point, but still valid. – Dave Newton Jun 08 '12 at 02:16
  • "I think we've finally arrived at a place we can all agree on." [No we haven't :)](http://www.youtube.com/watch?v=yTl9zYS3_dc) – mu is too short Jun 08 '12 at 04:11
0

params is a hash of variables that came over the HTTP request

DVG
  • 17,392
  • 7
  • 61
  • 88
  • oohh. Where do the params go? In the "path" (URI)? Probably. Could they go (come from) anywhere else? – boulder_ruby Jun 08 '12 at 00:54
  • It depends. On a get request like widgets/1 params[:id] would be the 1 passed in the URL. Or they may be sent over put or post for those kinds of requests. Further :id is a symbol which is like a string, but is a singleton do it only ever has one address in memory, so they get used all over the place in ruby/rails as keys – DVG Jun 08 '12 at 00:57
  • The semicolon doesn't go in the URL, the value that comes through the request gets assigned as the value in the hash, for which :id is the key. The route generally decides what parameters are accepted – DVG Jun 08 '12 at 01:01
  • @David URLs allow arbitrary characters, but that's a separate issue (and that's a colon). A `:` before a word in Ruby means it's a [Symbol](http://apidock.com/ruby/Symbol). – Dave Newton Jun 08 '12 at 01:08
0

params is a method returning a reference to a hash.

(But in general it's referred to as "the params hash", not "the params[] operator".)

Hashes can use symbols, as well as other data types, as hash keys:

[1] pry(main)> h = Hash.new
=> {}
[2] pry(main)> h[:foo] = 42
=> 42
[3] pry(main)> h[42] = :foo
=> :foo
[4] pry(main)> h
=> {:foo=>42, 42=>:foo}
[5] pry(main)> h[h[:foo]]
=> :foo

I'm not sure what you mean by "accepting :keys without values".

Symbols may also be used in other ways, for example, to refer to a method. An example would be when defining a before_filter:

class UsersController < ApplicationController

  before_filter :init

  def index
    # etc
  end

  private

    def init
      # Does some stuff
    end
end

The before_filter method takes a symbol indicating a method to run before calling an action.

Symbols are not references, they are symbols, and evaluate to themselves. See this SO question.

Community
  • 1
  • 1
Dave Newton
  • 158,873
  • 26
  • 254
  • 302