views:

45

answers:

2

im new to ruby and rails.

in RoR3 a controller inherits from the ActionController::Base

request.env["SERVER_ADDR"]

so request is a method in Base class (that is inside the ActionController module)?

what is env then and ["SERVER_ADDR"]?

would be great if someone could make a little code example...that would be very helpful to understand!

thanks!

+1  A: 

request.env["SERVER_ADDR"] can also be written as request().env()["SERVER_ADDR"]. So env is a method that is called without arguments on the object returned by request() and then you call [] on the object returned by that with the argument "SERVER_ADDR".

sepp2k
but why dont u have a dot after env()...request().env().["SERVER_ADDR"]
never_had_a_name
Because `foo[bar]` is nicer to read and write than `foo.[bar]`, so the creators of ruby decided that the syntax to call `[]` should be the former.
sepp2k
+1  A: 
request.env["SERVER_ADDR"]
  1. request is either

    a. dereferencing the local variable request or

    b. sending the message :request with no arguments to the implicit receiver self,

  2. env is sending the message :env with no arguments to the object obtained by dereferencing request or the object returned in response to sending the message :request to self in step 2,
  3. ["SERVER_ADDR"] is sending the message :[] with the argument "SERVER_ADDR" to the object returned in response to sending the message :env in step 2 and
  4. "SERVER_ADDR" is a string literal.

You could more explicitly write it like this:

self.request.env.[]("SERVER_ADDR")

or even more explicit like this:

self.request().env().[]("SERVER_ADDR")

and even full out:

self.send(:request).send(:env).send(:[], "SERVER_ADDR")
Jörg W Mittag
great explanation! +2! :) 1 invicible point
never_had_a_name