Strangeness with string concatenation and function return values

I get an error when running code snippet A, but if I change it to B, there's
no error. I'm not familiar enough with Ruby to know why this is. Could
someone explain this?

----- A -----
def ReturnString ( )
    return "abc"
end

File.open ( "junk.txt" , "w" ) do | f |
    f.write ( ReturnString ( ) + "\n" )
end

----- B -----
def ReturnString ( )
    return "abc"
end

File.open ( "junk.txt" , "w" ) do | f |
    s = ReturnString ( )
    f.write ( s + "\n" )
end

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

Mike Steiner

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

def return_string
  'abc'
end

File.open('junk.txt', 'w') do |f|
  f.puts return_string
end

···

On Tuesday 01 May 2007 22:27, Mike Steiner wrote:

I get an error when running code snippet A, but if I change it to B, there's
no error. I'm not familiar enough with Ruby to know why this is. Could
someone explain this?

----- A -----
def ReturnString ( )
    return "abc"
end

File.open ( "junk.txt" , "w" ) do | f |
    f.write ( ReturnString ( ) + "\n" )
end

--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemerriman.com/

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.

Harry

···

On 5/2/07, Jesse Merriman <jesse.d.merriman@gmail.com> wrote:

On Tuesday 01 May 2007 22:27, Mike Steiner wrote:
> I get an error when running code snippet A, but if I change it to B, there's
> no error. I'm not familiar enough with Ruby to know why this is. Could
> someone explain this?
>

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

def return_string
  'abc'
end

File.open('junk.txt', 'w') do |f|
  f.puts return_string
end

--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemerriman.com/

--

A Look into Japanese Ruby List in English