small medium large xlarge

Kevin_pragsmall
18 May 2012, 19:15
Kevin McCaughey (7 posts)

Hi,

I have already done some learning on Ruby before this book, but am getting very confused about the use of :’s (colons). For example, in Ch. 4, couple of paragraphs before regular expressions we have the line:

redirect_to action: 'show', id: product.id

The text says: “You can pass hashes as parameters on method calls. Ruby allows you to omit the braces… The following code fragment shows a two-element hash being passed to the redirect_to() method…”

So, redirect_to( param1, param2)? in that case I am really confused. Param 1 would be action: 'show' and… um… I’m lost, I just can’t get the syntax of this, and I am happily programming away in Ruby 1.9.

Could someone explain what is going on please and what part of the syntax is Rails and what is Ruby? I have read over it for the last half hour and I am just not seeing it.

Thanks :)

Kevin

Dave_gnome_head_isolated_pragsmall
18 May 2012, 23:50
Dave Thomas (390 posts)

It’s the same as

redirect_to { action: ‘show’, id: product_id }

So it takes one parameter, which is a hash. Since it is the last parameter, you can omit the { and }.

Dave

Kevin_pragsmall
19 May 2012, 11:41
Kevin McCaughey (7 posts)

Thanks for that. I am still a bit confused though. Is redirect_to a macro, a method (function). And why the braces? Usually it is parathesis round functions?

I think what is confusing me, in this as well as another Rails course I am studying, is the difference between what is Rails syntax and what is Ruby syntax. In the above, with the braces, it looks like it must be a non-Ruby construct of some sort.

Thanks again for the help - I am determined to get past this road block in my mind ;)

Kevin_pragsmall
19 May 2012, 16:18
Kevin McCaughey (7 posts)

Actually, I think I might be getting it, but…

Looking at the Rails API reference for the redirect_to (and I am still confused about is this a Ruby method or a rails construct, or even a Ruby class?):

It seems that redirect to is like a function where the first parameter is a hash enclosed in { }, followed by a second parameter which is a second hash? Or is it. Is the id: product_id here for the Status code as shown in the API? If not, how did it know it wasn’t if there were no braces to contain the hash?

Still confused :( I have to say that I find this the most difficult part of Rails: determining what is Ruby, what is Rails or what else might be macro or even an object? And also, default behaviour for processing parameters when braces and parenthesis are missing. It seems to default to something pretty odd here, or maybe I am madder than I thought ;)

I would be very grateful for any help/comments on this. I have been working in Ruby 1.9 and it’s the Rails syntax which seems to be getting in the way (that and some strange default behaviour).

Many thanks,

Kevin McCaughey

Dave_gnome_head_isolated_pragsmall
19 May 2012, 17:57
Dave Thomas (390 posts)

It is confusing—there’s a lot going on.

redirect_to is a method. It is part of Rails.

The call you are actually making is

redirect_to({action: ‘show’, id: product.id})

Let’s take this apart. You have a method name, then a parameter list between parentheses. Inside that parameter list, you have a literal hash between braces. That hash is the single parameter being passed to the method.

Now, Ruby lets you omit the braces if a hash literal is the last parameter to a method. So you can rewrite this as

redirect_to(action: ‘show’, id: product.id)

So far, so good. But Ruby also lets you remove the parentheses around method parameters if it is not ambiguous to do so. So you can further rewrite the method call as

redirect_to action: ‘show’, id: product.id

Still just one parameter, the literal hash.

My suggestion—you can always add the parens and braces for yourself. I’d even suggest it might be a good idea. Then, as you get more comfortable with the language, you may find yourself naturally leaving them off.

Kevin_pragsmall
30 May 2012, 22:48
Kevin McCaughey (7 posts)

Thanks Dave, it makes sense now. As I go through the book this is definitely my major problem (telling what is rails and what is Ruby).

I had learned Ruby quickly, but am also reading through your “Programming Ruby 1.9” at bedtime ;)

If I was to be given one wish, it would be for some kind of online parser that I could type these lines into and have it tell me what part belonged to what and where the optional brackets or braces would go. Maybe someone might think of a smart way to do this for beginners like me.

Thanks again for your help,

Kevin

You must be logged in to comment