5 ms·
Welcome to the wondrous world of dynamically typed languages like PHP and Ruby that are common in backend development. True as you know it can by any of true,
by pindab0ter 6y ago
Welcome to the wondrous world of dynamically typed languages like PHP and Ruby that are common in backend development.
True as you know it can by any of true, “true”, 1, “1”, >0 and most likely more.
- berkes 6y agoNot Ruby, but Rails. And there, it is handled by a clear and dedicated layer: ActiveModel::Type::Boolean.new.cast(value) The only thing you can complain about is that Rails loves implicit magic, which will call this layer for you. Without you needing to set it up, or call it yourself. Many people love this about Rails. (I prefer explicitness over magic.)
- rorykoehler 6y agoOnce you learn rails well enough it all becomes explicit because you can simply read the source code.
- berkes 6y agoExplicit, for me, means that I determine in the model the `published_at` is a DateTime. Not merely in the DB, and never magically derived from the name (_at=datetime, _on=date). But explicitely defined. E.g. `attribute(:published_at, DateTime, default: ->{DateTime.now})` Rails does not do this entirely "magic", though: it derives it from the database. And true: you can follow the logic along: the magic can be researched easily. But still: I really prefer a system in which I write all that boilerplace (my IDE/vim can do this for me really well) instead of relying on convention, heuristics and magic.
- Izkata 6y agoAll inputs in PHP start as strings because you can't tell for certain if a form input was supposed to be 1 or "1", so the language opts to not convert until you trigger it somehow. Combined with PDO for parametrized database queries, if this is PHP this error still should not happen.
- karmakaze 6y agoPython has dynamic types but stronger typing, e.g. Can't do `a + b` if `a` is a string and `b` is a number.
- BenjiWiebe 6y agoirb(main):001:0> true=="true" => false
- fomine3 6y agoRuby is very simple. Only false and nil is falsy.