Ruby syntax error with declarative concept of using variable

80 views Asked by At

I am trying to use the reactive programming concept in ruby, I have create two block of code:

1 Imperative

a = 5, b = 2
c = a + b
print c #=> 7

a = 2
print c #=> 7

2 Declarative

a := 5, b := 2
c := a + b
print c #=> 7

a := 2
print c #=> 4

However second example doesn't work for me and give the below error:

d.rb:1: syntax error, unexpected '=', expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END
a := 5, b := 2
    ^

Please anyone help me to find out the error in the code. Any suggestion will be highly appreciated.

I know the second one is pseudo code but one thing surprise me that top score person make it off topic? The second code can also be executed using Reactive Library and top score programmer don't aware about it.

1

There are 1 answers

4
SteveTurczyn On BEST ANSWER

The := is not valid Ruby.

The error message is because symbols are represented by leading colons so :example is a symbol (compare to "example" which is a string).

So encountering : Ruby expects a valid beginning character for a symbol, which would be any of...

@$_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Middle characters can be...

_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

And the last character can be...

!_=?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789

So = is never a valid symbol character.

The article you reference is showing pseudo-code not actual Ruby.