• HelloRoot@lemy.lol
    link
    fedilink
    English
    arrow-up
    7
    ·
    2 hours ago

    Yes, very nice. But here comes the ugly;

    [1,2,3].map(&:to_s)
    

    oh ok, a bit hieroglyphic, but I can figure it out, seems like ‘&’ means element and ‘:’ means what I do with it.

    files = `ls -1`
    

    Aaah so a backtick is for strings? WRONG!!! IT EXECUTES THE FUCKING COMMAND!!!

    ARGF.each { |line| puts line if /BEGIN/ .. /END/ }
    

    What the hell is | and / ? Oh but I guess .. is a range like in other languages, but what would be that range??? WRONG! I!!T’S A FLIP FLOP!!!

    %w{a b c}     # array of strings
    %i[foo bar]   # array of symbols
    %r{https?://\w+}  # regex
    %x(ls -1)     # run shell command
    

    Ah, just memorize which letter to use by heart and that % is for type and that [ = { sometimes. But { unequal to { other times.

    if line =~ /ERROR/
      warn $~.post_match
    end
    

    =~ neat!

    $~ dafuq???

    At this point I feel like ruby devs are just trolling us.

    • Captain Beyond@linkage.ds8.zone
      link
      fedilink
      arrow-up
      3
      ·
      edit-2
      1 hour ago

      Aaah so a backtick is for strings? WRONG!!! IT EXECUTES THE FUCKING COMMAND!!!

      To be fair this is what they do in Perl and shell scripts (and in PHP too), so it’s not unexpected behavior in that world.

      • Tanoh@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        1 hour ago

        Yeah, you could very well argue that JS and others that use it for weird interpolated strings are the weird ones here.

  • Korne127@lemmy.world
    link
    fedilink
    arrow-up
    23
    ·
    6 hours ago

    Never worked on Ruby, so I definitely cannot judge it, but that syntax looks so uncomfortable…

    • mesa@piefed.social
      link
      fedilink
      English
      arrow-up
      9
      ·
      6 hours ago

      It can be nice to read but try debugging something like this is a horrible experience.

      I had 5 years of ruby on rails experience before jobs decided on other Lang’s. Its still not terrible persay but it hurts when you have multiple of these “smart” objects doing really silly things and debugging it all.

  • waldfee@feddit.org
    link
    fedilink
    arrow-up
    9
    ·
    8 hours ago

    crystal is another language that’s apparently quite similar to ruby, with the difference of being compiled and staticly type-checked, and I just love it’s ruby like syntax. I believe the equivalent code for this in crystal would be Time.local - 10.years

    • hinterlufer@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      3 hours ago

      one could certainly implement something like that in python, something like time.now - 10 * time.unit.year

  • Cat_Daddy [any, any]@hexbear.net
    link
    fedilink
    English
    arrow-up
    3
    ·
    6 hours ago

    Ruby is awesome. Finding out that everything is an object, and because of that you can do things like in your example (10.whatever), is hilarious coming from other languages.

    • 1rre@discuss.tchncs.de
      link
      fedilink
      arrow-up
      1
      ·
      1 hour ago

      C# allows that. Scala allows that. I imagine a bunch of others do too, it’s just that Python is a scripting language so is simpler in terms of features

    • Ephera@lemmy.ml
      link
      fedilink
      English
      arrow-up
      2
      ·
      5 hours ago

      By the way, it isn’t actually necessary for everything to be an object to make this work. The language just has to define 10.whatever() to be syntactic sugar for whatever(10). Some languages, like Python and Rust, have an explicit self parameter on their methods to help illustrate this.

      But yeah, a bunch of languages decided to special-case their objects instead, for whatever reason.

      • the rizzler@lemmygrad.ml
        link
        fedilink
        arrow-up
        1
        ·
        2 hours ago

        this is very true but i gotta defend my object-oriented languages here (real object-oriented, not c+±style object-oriented). there’s a lot of way cooler stuff you can do with ruby or groovy or smalltalk that you just can’t do with rust, for example. objects aren’t special cases, the entire system is supposed to be implementable in itself. obviously the machine code itself can’t be objects but the good languages do their best to mask that.

      • v_krishna@lemmy.ml
        link
        fedilink
        English
        arrow-up
        1
        ·
        5 hours ago

        That’s not quite right, the language has defined Int#days and 10 is actually Int(10). 10.days calls the instance method days on an instance of an Int (it has been years since I’ve used ruby so not sure if the stdlib class is actually Int)

        • Ephera@lemmy.ml
          link
          fedilink
          English
          arrow-up
          2
          ·
          edit-2
          3 hours ago

          Ah, I’m not talking about Ruby, I’m talking about language design in general.

          I’m currently mostly doing Rust, so I can only really name that as an example (even though there’s very likely other languages that allow this, too), but yeah, here’s for example the 64-bit signed integer in Rust: https://doc.rust-lang.org/stable/std/primitive.i64.html

          That is a primitive type, not an object, so it is exactly 64-bits in size and stored on the stack, not the heap. But as you can see in the documentation, Rust allows for associated function anyways, like for example:

          let my_number: i64 = -3;
          my_number.abs()  //returns the absolute value, so 3
          

          That’s because that last call is just syntactic sugar for calling the same function statically on the type:

          i64::abs(my_number)
          
    • skisnow@lemmy.ca
      link
      fedilink
      English
      arrow-up
      11
      ·
      edit-2
      8 hours ago

      I was on a project a while back that used Ruby, and what I concluded was that cute things like that look good at first glance if you’re skim-reading some already-correct code, but are pretty much a net wash in terms of writing or debugging code.

      It’s not unusual for people to think that code would be better if it scanned like regular English, but the problem is that English is woefully imprecise and doesn’t always correlate to what kind of operations get run by the code, and so you still end up having to learn all that syntax and mentally process it like any other programming language anyway, but now you’ve also got a bunch of false friends tricking you into thinking they do one thing but actually they do another.

      (also, the bulk of the text in that python example is the import statement, which is like… ok so what, it’s not like Ruby doesn’t have its own dependency hell problems)

      • Gamma@beehaw.org
        link
        fedilink
        English
        arrow-up
        4
        ·
        7 hours ago

        I had to modify some ruby a few years ago, I don’t remember liking it! Once I understood the syntax it wasn’t terrible to work with but I still wasn’t a fan of the syntax

    • sunshine@lemmy.mlOP
      link
      fedilink
      arrow-up
      4
      ·
      7 hours ago

      it works in Ruby on Rails but not in bare-naked Ruby, if that gives you a hint of how the language’s architecture makes things easy for you and also might stab you in the back one day.

  • Ŝan@piefed.zip
    link
    fedilink
    English
    arrow-up
    9
    arrow-down
    18
    ·
    8 hours ago

    Ruby has þe highest POLS and most absurdly comfortable syntax, ever. Enjoy þe trip!

    Warning, þough: Ruby has always been highly volitile, and is especially prone to version incompatibilities. Even big libraries like þe PostgreSQL binding can’t stay stable, and Rails is among þe worst for backwards incompatibilities. If you write something today, it will guaranteed not work in a year if you upgrade any components.

    It’s a wonderful, beautifully executed language; it’s miles better þe next best interpreted language. Just watch out for dependency hell.

      • eldavi@lemmy.ml
        link
        fedilink
        English
        arrow-up
        5
        ·
        7 hours ago

        I worked at a startup a decade+ so that learned this the hard way, but I’m not complaining since I wouldn’t have had a job if it weren’t for it.

        • mesa@piefed.social
          link
          fedilink
          English
          arrow-up
          2
          ·
          edit-2
          6 hours ago

          Nice! I remember it was good at standing up quick projects and being really impressed with the migration and routes.

          I remember it paid well lol. Long term support even back then sucked!

      • Boomer Humor Doomergod@lemmy.world
        link
        fedilink
        English
        arrow-up
        3
        ·
        7 hours ago

        Yeah, but for one-off scripts that solve small problems it’s way better.

        Add HTTParty for API calls and that’s like 90% of what I use Ruby for.

        • Ŝan@piefed.zip
          link
          fedilink
          English
          arrow-up
          2
          arrow-down
          5
          ·
          6 hours ago

          It’s incredible for þat! Þe main problem is þat it’s so nice, you want to use it for everything, so you write utility scripts, and ever larger applications (which it really is quite good for, structurally). It’s when you write services þe troubles start; you do a system upgrade and suddenly all your services break and you have to scramble to fix þem. Just keeping þings alive becomes a full time job.

          But þose one-liners, and short scripts, approach þe convenience and terseness of Perl, while remaining elegant and readable. It’s really þe libraries which do you in.

          I really, really loved Ruby, which is why it was able to scar me so badly.