Jason Hsu, Android developer
2013-Feb-20 17:57 UTC
Calling a function: one space difference between failing and succeeding
I have a function defined as follows: def download_file (url1, url2, file1, file_age_max_hours) puts (url1) puts (url2) puts (file1) puts (file_age_max_hours) end The following command works: download_file("Hello", "World", "Rubyists", 34) The following command fails: download_file ("Hello", "World", "Rubyists", 34) The only difference is the space before the "(". Why is the presence or absence of this space so significant? This was never a problem for me in Python. (On the other hand, I quickly learned to NEVER use tabs to indent when programming in Python.) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1YGlD7hsz5UJ. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-Feb-20 21:25 UTC
Re: Calling a function: one space difference between failing and succeeding
On 20 February 2013 17:57, Jason Hsu, Android developer <jhsu802701-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a function defined as follows: > > def download_file (url1, url2, file1, file_age_max_hours) > puts (url1) > puts (url2) > puts (file1) > puts (file_age_max_hours) > end > > The following command works: > download_file("Hello", "World", "Rubyists", 34) > > The following command fails: > download_file ("Hello", "World", "Rubyists", 34) > > The only difference is the space before the "(". Why is the presence or > absence of this space so significant? This was never a problem for me in > Python. (On the other hand, I quickly learned to NEVER use tabs to indent > when programming in Python.)Because this is Ruby not Python. My understanding of why this is so (which may be wrong or at least incomplete) is because you do not need the parentheses at all, so you could say download_file "Hello", "World", "Rubyists", 34 so if you have a space and a "(" then the space starts the parameter list and then along comes a "(" which may now have a different meaning, dependant on context. So the best thing is just to remember not to put a space after the method name if you are using parentheses. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Jordon Bedwell
2013-Feb-20 21:47 UTC
Re: Calling a function: one space difference between failing and succeeding
On Wed, Feb 20, 2013 at 3:25 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 20 February 2013 17:57, Jason Hsu, Android developer > <jhsu802701-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I have a function defined as follows: >> >> def download_file (url1, url2, file1, file_age_max_hours) >> puts (url1) >> puts (url2) >> puts (file1) >> puts (file_age_max_hours) >> end >> >> The following command works: >> download_file("Hello", "World", "Rubyists", 34) >> >> The following command fails: >> download_file ("Hello", "World", "Rubyists", 34) >> >> The only difference is the space before the "(". Why is the presence or >> absence of this space so significant? This was never a problem for me in >> Python. (On the other hand, I quickly learned to NEVER use tabs to indent >> when programming in Python.) > > Because this is Ruby not Python. My understanding of why this is so > (which may be wrong or at least incomplete) is because you do not need > the parentheses at all, so you could say > download_file "Hello", "World", "Rubyists", 34 > so if you have a space and a "(" then the space starts the parameter > list and then along comes a "(" which may now have a different > meaning, dependant on context. So the best thing is just to remember > not to put a space after the method name if you are using parentheses.the space + () creates a new closure and that closure causes an error because you have a bunch of commas outside of context. Colin is right, I just wanted to elaborate that it creates a new closure. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.