voltrb / volt

A Ruby web framework where your Ruby runs on both server and client
MIT License
3.22k stars 196 forks source link

Comparing string with promise #336

Closed Mortaro closed 8 years ago

Mortaro commented 8 years ago

not sure if its the expected behavior but when i compare a column with a string it fails since its actually a promise.

this is returning false

store._books.last._title == "Title"

while this is returning true

store._books.last._title.then {|t| t == "Title"}

if i'm not doing something wrong, patching this would be really handy for my views!

RickCarlino commented 8 years ago

Hi @Mortaro here's what's really going on:

  1. A promise wraps around a resolved value
  2. If you call a method on a promise, and the promise does not implement that method, it will forward that method to the resolved object later on when it is resolved.
  3. If you call a method on a promise that is already implemented on the promise object itself, it will not be forwarded to the resolved object. It will get called on the promise object itself.

This feature is called "Promise method forwarding". In your case, it does not work because == is already implemented on the Promise class. Conversly, it DOES work on the first example because Promise does not implement a method _title.

See this blog / video for more info. Look at the subheading Forwarding 'gotchas'.

Mortaro commented 8 years ago

@RickCarlino Thanks for the quick reply!

wow that blog and this framework are awesome, makes me like coding again :)

still on the promises subject, i noticed that opal only allows to call "then" on a promise once without triggering an error. Googling around i saw a post saying that i should call "then" over the previous callback, but that sounds a bit limiting, does volt provide a undocumented alternative for that, or should i change my mindset about promises?

Thanks again

ryanstout commented 8 years ago

@Mortaro were working on changing opals promise library or maybe replacing it so you can call them multiple times.

Mortaro commented 8 years ago

@ryanstout thanks for the info!