34
Root
3y

Ruby’s fanciness bit me in the butt today. It’s pretty rare, but often confusing AF when it happens.

array = [1, 2, 3, 4, 5, 6, 7]
array.count +1 +2
# => 1
What the fuck?

array.count +1 +2 +3
# => 1
What the fuck?

+1 +2 +3
# => 6
Okay.

(array.count +1 +2 +3)
# => 1
What the fuck?

(7 +1 +2 +3)
# => 13
Okay...

array.count + 1 + 2 + 3
# => 13
Alright, so spaces matter here...?

((array.count) +1 +2 +3)
# => 13
But not here!? ... Oh. I think I know what’s going on.

Array#count
Returns the number of elements. If an argument is given, counts the number of elements which equal it using ==

Well fuck me.

Ruby is seeing `array.count(+1+2+3)` instead of `array.count()+1+2+3` since `+1` is a value, not an operator followed by a value as is the case with `+ 1`.

Now, why was I using +1 +2 instead of adding some spaces like I normally would? So they would match what was in the comment next to them for easier reference. Heh.

Future dev, I did this for you! So this is all your fault. :|

Comments
  • 3
    I guessed the issue by the first one given that I found it weird you were using the positive operator. I didn't know that ruby had non-paren invocations but I guessed that, if that were the case (seeing as how coffeescript had them and coffeescript was largely inspired by Ruby) then that would be happening lmfaoooooooo

    Wow that must have been a headache.
  • 4
    @junon It didn’t take me long to figure it out, but longer than it should have.

    I’m so burned out on this damned screwdriver I literally can’t focus on either the work or even the freaking screen itself. Everything is just a blur.
  • 2
    Quirks and unknowns may break my codes, but Ruby will never deter me.

    Thanks for the TIL!
  • 2
    The fuck ? The screwdriver project is still on ? *still killing people

    Fuck me, maybe it’s time for cork screw Instead.
  • 1
  • 0
    Sounds like Ruby is a yet another instance of JavaScript.

    Fuck that
  • 0
    @iiii How would this happen in Javascript?
  • 1
    @junon I mean overall arcane unintuitive operation.
  • 5
    @iiii Nah. The problem described here comes from optional parentheses for invocations. In that context, what happens makes sense and is consistent.

    JS, on the other hand doesn’t even know what consistency is. All syntax is some random shit behavior depending on numerous conditions.
  • 3
    @Lensflare namely, which version of Babel is being used, or which bundler you're using.

    I fucking hate what the Javascript ecosystem has become.
  • 1
    What I like about Haskell is how explicit it is about invocation, parameters and order of operations.

    What I don't like about Haskell is how many operators there are to denote all the orders of operations and fixities. 😆

    f . g . h $ x == f(g(h(x)))

    (*2) <$> [1..3] == [2,4,6]

    [(*10),(+2)] <*> [2,3,4] == [20,30,40,4,5,6]

    flip f x y == f y x

    >>> == flip (.)

    (f >>> g) x == g(f(x))

    (f &&& g) x == (f x, g x)

    a <| [b, c] == [a,b,c]

    (f ?? x) y == f y x

    both **~ 2 $ (3,2) == (9,4)
  • 1
    @bittersweet I can't read any of that nonsense, that's why I don't like Haskell. It uses way too much symbology and creates terse, unreadable code IMO.

    I'm a pretty firm believer that a language should be at least understandable but someone who knows a few languages even if they don't know that language. Haskell does not fit that bill.
  • 0
    @bittersweet I like the idea of a function composition invocation operator (the dot in haskell, it seems).

    There is the famous F# pipe forward operator but none of the popular languages have an equivalent of that.

    Maybe it’s because it only works with functions taking and returning exactly one value, but still I’d like to have an operator doing that. Fortunately we can build such an operator in languages that support operator overloading (or custom operators). 🤘
  • 1
    @junon Haskell can totally fit that bill — you can alias any operator.

    But yeah, in general the community works by convention, and many users of Haskell are academics who like symbolic notations.
  • 1
  • 1
    @Lensflare @junon

    So yeah it is actually all about composition. Take this sample:

    https://github.com/alpmestan/...

    What does it do?

    It defines a struct (data) with three Text fields, and calls it Upload.

    It defines a function called table, which parses HTML tables. The parameter is a List of Nodes, and Maybe it returns our Upload struct with its Text fields

    Then recentPackages is a function which takes a Response Bytestring, and returns a List of [Maybe Upload].

    It composes methods to read the body from a response, a HTML parser, traversal methods and our table function together.

    Then at the bottom, it just prints it all into the CLI.

    As far as web scraping goes, I think that's pretty readable -- you can almost just skip over all the silly operator symbols and still understand what it is about.
  • 1
    @bittersweet that’s nice for DSLs!
  • 1
    @Lensflare

    http://hackage.haskell.org/package/...

    import BASIC

    main = runBASIC $ do

    10 PRINT "Hello World"

    20 GOTO 10

    But also:

    https://hackage.haskell.org/package...
  • 1
    @bittersweet that escalated quickly 😅
  • 1
    I hate everything except for C++.

    People who constantly tell me how easy scripting languages are never mention this kind of BS
  • 1
    @YADU Running into issues like this is honestly pretty rare.

    C > Ruby > Assembly > everything else.
  • 0
    @Root

    For me it's more:

    C > C++ > OCaml > Bash > everything else
  • 0
    @YADU Bash is awful :(
  • 0
    @Root so are most languages but Bash at least lets me do file stuff.
  • 0
    @YADU Ruby is not a scripting language
  • 2
    @bioDan It’s certainly not a compiled language.
  • 0
    @Root we both know its an interpreted language. Bash however is a command/scripting language. I was addressing the comment about people mentioning "how easy it was using scripting languages" that @YADU was implying about Ruby
  • 1
    @bioDan Fair! Thanks for the clarification.
  • 0
    @Root

    > It’s certainly not a compiled language.

    I wouldn't say "certainly" 🤓

    https://bugs.ruby-lang.org/projects...

    🤷
  • 1
    @Root

    Also obviously it's:

    Rust > Haskell > Almost Everything Else > Bash > Ruby 🙃
  • 1
    @bittersweet ohhh until now i thought you know what's good 😉
  • 1
    @bioDan everything that doesn't compile is a scripting language to me
  • 1
    @YADU pitty. There's a whole world of different language types out there.
  • 1
    @YADU So when YOU write C code.... it's a scripting language?

    🙃

    (sorry, I'll show myself out)
  • 1
    @bittersweet hey my code mostly compiles.

    It's the runtime errors that get me ;)
  • 2
    @YADU

    I hate everything except Haskell & Rust.

    People who tell me how great C++ is never mention this kind of runtime error BS

    (Yeah at this point I'm just trolling. But it's justified trolling dammit!)
Add Comment