0

This code should be valid (code snippet is from a book). I am trying to learn the ruby language and I am getting an unexpected error. The author was using ruby v1.8.4 -- I am using 2.0. I suspect the version is to blame.

lineWidth = 40
str = '--> Word <--'

puts str.ljust lineWidth
puts str.rjust lineWidth
puts str.center lineWidth
puts str.ljust (lineWidth/2) + str.rjust (lineWidth/2)

ruby version 2.0 p195

*Error is

c5.rb:7: syntax error, unexpected ( arg, expecting end-of-input
puts str.ljust (lineWidth/2) + str.rjust (lineWidth/2)

*Output should be

--> Word <--
                            --> Word <--
              --> Word <--
--> Word <--                --> Word <--

Can someone tell me why this is returning an error? Thank you!

1
  • Looks like precedence issues, try puts ((str.ljust (lineWidth/2)) + (str.rjust (lineWidth/2))), if that works try peeling off parens until you find the culprit. Commented Jun 8, 2013 at 3:08

1 Answer 1

2

Just remove the spaces between method name and parenthesis:

puts str.ljust(lineWidth/2) + str.rjust(lineWidth/2)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.