never_had_a_name
never_had_a_name

Reputation: 93306

class declaration for ruby

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!

Upvotes: 1

Views: 207

Answers (2)

Jörg W Mittag
Jörg W Mittag

Reputation: 369624

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")

Upvotes: 1

sepp2k
sepp2k

Reputation: 370465

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".

Upvotes: 1

Related Questions