Very basic question

I know this may be a very basic question, but confusing me.

If I write the following for example in the controller:

@x = App.new(params[:x])

I know that I'm trying to create a new object here, but how can I read the params[:x] part.

I know also that "params" is a Hash table in the controller, but what is the following statement trying to say when passing [:x] to params? What is this value? From where will I retrieve it?

Thanks.

SW Engineer wrote:

I know this may be a very basic question, but confusing me.

If I write the following for example in the controller:

@x = App.new(params[:x])

I know that I'm trying to create a new object here, but how can I read the params[:x] part.

I know also that "params" is a Hash table in the controller, but what is the following statement trying to say when passing [:x] to params? What is this value? From where will I retrieve it?

Do you understand how hashes (not "Hash tables"!) work in Ruby? If not, go read the Pickaxe book or other basic reference, then come back if you have further questions.

Or are you asking where the params hash gets assigned to in the first place? If so, then that's done by the Rails framework. I believe the Rails Guides will tell you more about that.

Thanks.

Best,

Thanks @Marnen.

I'm asking just how to read this:

@x = App.new(params[:x])

Like saying, "Create a new object..........".

Create a new App object, passing params[:x] to the initialize method. As Marnen said this is very basic Ruby. Get the Pickaxe book and work through it.

Colin

Please quote when replying.

SW Engineer wrote:

Thanks @Marnen.

I'm asking just how to read this:

@x = App.new(params[:x])

Like saying, "Create a new object..........".

And I pointed you to where you can find the answers. Was the information in those sources insufficient? If so, what part is still problematic for you?

Best,

Thanks a lot.

Thanks @Marnen.

I'm asking just how to read this:

@x = App.new(params[:x])

params[:x] means (assuming that params is a Hash) get the value for the key :x

Fred

Thanks @Fred.

Now, I'm getting it better.

The last thing is, will the value be passed as a parameter to [:x] when creating a new object?

_Please_ go and read up on Ruby and go through some basic Ruby tutorials. Then you will look back on the question you asked and be embarrassed that every time someone googles and comes across it they will think how silly it is.

Colin

Thanks @Fred.

Now, I'm getting it better.

The last thing is, will the value be passed as a parameter to [:x] when creating a new object? -- Posted viahttp://www.ruby-forum.com/.

This is both Rails basics and Ruby basics. 'params' is a hash and acts like a hash. Here is a quick example from the console that I hope helps but you would actually save time and learn more by reading up on the subject:

params = {}

=> {}

params.class

=> Hash

params[:x] = 'SW Engineer'

=> "SW Engineer"

params.inspect

=> "{:x=>\"SW Engineer\"}"

puts params[:x]

SW Engineer

As you can see ':x' is the key to the value 'SW Engineer'. In Rails that value would be coming from a field in your page called 'x'.