-2

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.

Dinesh Saini
  • 2,917
  • 2
  • 30
  • 48

1 Answers1

3

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.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • Any other way to differentiate between imperative & declarative approach. – Dinesh Saini Aug 12 '15 at 05:36
  • 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. – Dinesh Saini Aug 13 '15 at 04:49
  • You're talking about ReactiveX ? It's not really adaptable to the pseudo-code you were trying to run. Basically, it was off-topic because you misunderstood the syntax of Ruby. There is a good, fundamental question there, but phrased badly. Instead of "why doesn't `c := a + b` work?" you could've asked "how can I code `c = a + b` so that `c` dynamically changes when `a` or `b` changes?" – SteveTurczyn Aug 13 '15 at 08:12
  • Could you please edit the question instead of off topic. – Dinesh Saini Aug 14 '15 at 05:36