r/learnruby • • Jan 03 '16
What does the white space in the following code represent ?
Here is the code in question
def registration_confirmation(user)
recipients user.email
from "webmaster@example.com"
subject "Thank you for Registering"
body :user => user
endThis is the code from a railcasts episode on sending an email. My question is what dfrom "webmaster@example.com" mean ? Shouldn't it be from = "webmaster@example.com". What does the white space here represent ?
2
Upvotes
5
u/slade981 Jan 03 '16
I'm no pro, but I'm pretty sure it doesn't mean anything. It's just whitespace. Ruby ignores it so what the line really represents is:
As if it had a a single space. The extra spaces are to line everything up and make it look neat and readable. Look at it without the whitespace.
It's a bit harder to read. That's all the whitespace is.
Now as for the why isn't there an equals? I can only guess, but that's most likely because something has already been set up somewhere to take in the data that way. The "from" in this case is probably a method and not a variable like it seems. So it's really calling a method with a single parameter.
1
u/jrochkind Jan 22 '16 edited Jan 22 '16
Why isn't it an equal sign? Because it's a method call, calling methods called
recipients,from, etc. Same thing as:Whatever object
registration_confirmationis defined on has methodsreceipients,from,subject, andbody, and you're calling those methods with those arguments. That's it.In ruby you can call methods without parens if you want.
Then in your code example extra whitespace is put in to make everything line up nice and be readable, but it doesn't make a difference either. It's just a method call.
Okay, the next step is even if there was an equal sign, it would still be a method call. If the containing object had defined methods like:
Then you'd call it like:
Ruby also gives you that nice syntax for methods ending in
=, you can call them in a special way that looks like assignment, but it's actually just a method call still.And the last step is there might not really be a method
recipientsin the containing object, because the author might have used special meta-programming techniques to respond to the method-call (or "message")recipientswithout actually defining a method.But that's all just extra. Whenever you see
something elsein ruby, think "Calling the methodsomethingwith the argumentelse, same assomething(else).Also, Rails is the worst way to learn ruby, Rails does too many confusing things.