Forum: Ruby strangeness with string concatenation and function return va

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Mike S. (Guest)
on 2007-05-02 06:28
(Received via mailing list)
I get an error when running code snippet A, but if I change it to B,
no error. I'm not familiar enough with Ruby to know why this is. Could
someone explain this?

----- A -----
def ReturnString ( )
    return "abc"
end ( "junk.txt" , "w" ) do | f |
    f.write ( ReturnString ( ) + "\n" )

----- B -----
def ReturnString ( )
    return "abc"
end ( "junk.txt" , "w" ) do | f |
    s = ReturnString ( )
    f.write ( s + "\n" )

If it makes any difference, I'm running Ruby under Windows.

Mike S.
Jesse M. (Guest)
on 2007-05-02 07:05
(Received via mailing list)
On Tuesday 01 May 2007 22:27, Mike S. wrote:
>     f.write ( ReturnString ( ) + "\n" )
> end

You can't have that space between ReturnString and the parenthesis (you
like lots of spaces, eh?). Here's one way of doing it that should work:

def return_string
end'junk.txt', 'w') do |f|
  f.puts return_string
Harry Spidor (Guest)
on 2007-05-02 07:26
(Received via mailing list)
On 5/2/07, Jesse M. <removed_email_address@domain.invalid> wrote:
>   'abc'

Jesse explained the problem.
I ran your code and there were no errors in one version but there were
warnings about the spaces before parentheses. It is a good idea to
heed warnings and try to make them go away.

This topic is locked and can not be replied to.