Beehaw
  • Communities
  • Create Post
  • search
    Search
  • Login
  • Sign Up
 sunshine   ( @sunshine@lemmy.ml )  to Programmer Humor@lemmy.mlEnglish · 4 months ago

I'm new to using Ruby and this tickled me pink

lemmy.ml

message-square
46
link
fedilink
166

I'm new to using Ruby and this tickled me pink

lemmy.ml

 sunshine   ( @sunshine@lemmy.ml )  to Programmer Humor@lemmy.mlEnglish · 4 months ago
message-square
46
link
fedilink
alert-triangle
You must log in or # to comment.
  •  illusionist   ( @illusionist@lemmy.zip ) 
    link
    fedilink
    arrow-up
    16
    ·
    4 months ago

    Looks like one is defined as years and one as days. 10 years does not necessarily equal 365 times 10.

    •  kureta   ( @kureta@lemmy.ml ) 
      link
      fedilink
      arrow-up
      2
      ·
      4 months ago

      In fact, it would never equal 365 * 10 days.

  •  Kazumara   ( @Kazumara@discuss.tchncs.de ) 
    link
    fedilink
    arrow-up
    14
    ·
    4 months ago

    The python version seems buggy as fuck. Depending on which year you run it it’s off by 1-3 days

    •  dumples   ( @dumples@midwest.social ) 
      link
      fedilink
      English
      arrow-up
      8
      ·
      4 months ago

      Python does have a year option that they are not using. Depending on the application I would use 365 for a year to get a consistent number of days.

      •  sunshine   ( @sunshine@lemmy.ml ) OP
        link
        fedilink
        arrow-up
        5
        ·
        4 months ago

        I did look up the help for that function to make this meme but I must have missed that option. in my defense I’ve only been using Python for like 10 years

      •  Arthur Besse   ( @cypherpunks@lemmy.ml ) 
        link
        fedilink
        English
        arrow-up
        1
        ·
        4 months ago

        Python does have a year option that they are not using.

        No, it doesn’t:

        help(datetime.timedelta)
        Help on class timedelta in module datetime:
        
        class timedelta(builtins.object)
         |  Difference between two datetime values.
         |
         |  timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
         |
         |  All arguments are optional and default to 0.
         |  Arguments may be integers or floats, and may be positive or negative.
        
        •  dumples   ( @dumples@midwest.social ) 
          link
          fedilink
          English
          arrow-up
          1
          ·
          4 months ago

          Your right. I was thinking about pd.DateOffset()

    •  comfy   ( @comfy@lemmy.ml ) 
      link
      fedilink
      arrow-up
      1
      ·
      4 months ago

      That sounds serious, can you give some example values we can test?

      •  mrgoosmoos   ( @mrgoosmoos@lemmy.ca ) 
        link
        fedilink
        English
        arrow-up
        9
        ·
        4 months ago

        look I’m not trying to be a dick or anything, but do you not know about leap years and which years they are?

        •  comfy   ( @comfy@lemmy.ml ) 
          link
          fedilink
          arrow-up
          1
          ·
          4 months ago

          edit: just realized it was the python and not ruby example, I was very tired and distracted when I was reading this thread.

      •  Kazumara   ( @Kazumara@discuss.tchncs.de ) 
        link
        fedilink
        arrow-up
        8
        ·
        4 months ago

        Sure, here’s one example for each case:

        1 day off: 3650 days before 1907-01-01 is 1897-01-02

        2 days off: 3650 days before 2027-01-01 is 2017-01-03

        3 days off: 3650 days before 2025-01-01 is 2015-01-04

      •  gonzo-rand19   ( @gonzo-rand19@moist.catsweat.com ) 
        link
        fedilink
        arrow-up
        3
        ·
        4 months ago

        29 February 2028, 29 February 2032, 29 February 2036…

  •  Daniel Quinn   ( @danielquinn@lemmy.ca ) 
    link
    fedilink
    English
    arrow-up
    9
    ·
    4 months ago
    from datetime import datetime
    from dateutil.relativedelta import relativedelta
    
    print(datetime.now() + relativedelta(years=10))  # 2035-08-24 12:02:49.795177
    
    •  kureta   ( @kureta@lemmy.ml ) 
      link
      fedilink
      arrow-up
      1
      ·
      4 months ago

      Didn’t know about relativedelta. Thanks!

  •  waldfee   ( @waldfee@feddit.org ) 
    link
    fedilink
    arrow-up
    8
    ·
    4 months 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

  •  HelloRoot   ( @HelloRoot@lemy.lol ) 
    link
    fedilink
    English
    arrow-up
    8
    ·
    edit-2
    4 months ago

    Edit:

    To clarify, I looked at existing online ruby code and gave it a small test for readability. It may be outdated, use uncommon syntax, bad practice or be full of individual developer quirks - I wouldn’t know. I did that because I wanted to highlight some weaknesses of the language design that turned me away from ruby years ago. https://en.wikipedia.org/wiki/Principle_of_least_astonishment


    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. There are always multiple ways to do the same thing. Every example from above also has a tidy and readable way to do it. But the alternative ways become progressively more shorthand, unreadable and unintuitive.

    •  Captain Beyond   ( @beyond@linkage.ds8.zone ) 
      link
      fedilink
      arrow-up
      14
      ·
      4 months 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.

      •  HelloRoot   ( @HelloRoot@lemy.lol ) 
        link
        fedilink
        English
        arrow-up
        5
        ·
        edit-2
        4 months ago

        I’m way happier debugging “200 char wide class name + 50 line of boilerplate” code written in java that verbosely and expressively does the same thing compared to deciphering single symbol hieroglyphs in shell esque scripts where I have to pay attention which way the ticks are pointing.

    •  Oriel Jutty :hhHHHAAAH:   ( @barubary@infosec.exchange ) 
      link
      fedilink
      arrow-up
      3
      ·
      4 months ago

      Does Ruby require the use of [] and {} there? Because those %w/%i/etc things look like custom quoting operators and at least in Perl you can use any delimiter you want: qw(a b c) is a list of strings, but so are qw+a b c+ and qw;a b c;.

      •  HelloRoot   ( @HelloRoot@lemy.lol ) 
        link
        fedilink
        English
        arrow-up
        3
        ·
        4 months ago

        Yes, but why?

        https://en.wikipedia.org/wiki/Principle_of_least_astonishment

  •  ArcaneSlime   ( @ArcaneSlime@lemmy.dbzer0.com ) 
    link
    fedilink
    arrow-up
    7
    ·
    4 months ago
    10.years.ago
    On.a.cold.dark.night
    There.was.someone.killed
    'Neath.the.town.hall.lights
    There.were.few.at.the.scene
    Though.they.all.agreed
    That.the.slayer.who.ran
    Looked.a.lot.like.me
    
    •  AnarchistArtificer   ( @AnarchistArtificer@slrpnk.net ) 
      link
      fedilink
      English
      arrow-up
      2
      ·
      4 months ago

      This is delightful

      •  ArcaneSlime   ( @ArcaneSlime@lemmy.dbzer0.com ) 
        link
        fedilink
        arrow-up
        1
        ·
        4 months ago

        Great song, despite the subject matter being somewhat disagreeable to me. One of the most covered country songs of all time, written by one of the best to touch the genre, Lefty Frizzell.

        https://www.youtube.com/watch?v=50k18gL76AU

        Personally I think he should have just spoken up. And so should she have. They never should have been doing that in the first place but it wasn’t worth taking the rap for murder. Still one of the best songs ever.

  •  Ŝan • 𐑖ƨɤ   ( @Sxan@piefed.zip ) 
    link
    fedilink
    English
    arrow-up
    6
    ·
    4 months 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.

    •  rockSlayer   ( @rockSlayer@lemmy.blahaj.zone ) 
      link
      fedilink
      English
      arrow-up
      9
      ·
      4 months ago

      What’s with the “th” symbol?

      •  Ŝan • 𐑖ƨɤ   ( @Sxan@piefed.zip ) 
        link
        fedilink
        English
        arrow-up
        12
        ·
        4 months ago

        Just messing wiþ LLM scrapers harvesting training material.

        •  belluck   ( @belluck@lemmy.blahaj.zone ) 
          link
          fedilink
          arrow-up
          3
          ·
          4 months ago

          Btw, þ is supposed to be used for the “hard” th (Wikipedia article for the corresponding phoneme with audio sample).

          The “soft” th has another letter, ð (Wikipedia).

          Wikipedia about the usage of ð (and a bit of þ) in old English

        •  I Cast Fist   ( @ICastFist@programming.dev ) 
          link
          fedilink
          arrow-up
          1
          ·
          4 months ago

          Why not use “zhe” or “ze”, so at least you sound like a posh continental yuropeean?

    •  ryannathans   ( @ryannathans@aussie.zone ) 
      link
      fedilink
      arrow-up
      5
      ·
      4 months ago

      In other words don’t use it for projects that need to be maintained or have longevity

      •  eldavi   ( @eldavi@lemmy.ml ) 
        link
        fedilink
        English
        arrow-up
        3
        ·
        4 months 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   ( @mesamunefire@piefed.social ) 
          link
          fedilink
          English
          arrow-up
          2
          ·
          4 months 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!

          •  eldavi   ( @eldavi@lemmy.ml ) 
            link
            fedilink
            English
            arrow-up
            1
            ·
            4 months ago

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

            i saw it as a reason to be thankful that i was employed. lol

            •  mesa   ( @mesamunefire@piefed.social ) 
              link
              fedilink
              English
              arrow-up
              1
              ·
              4 months ago

              nice.

  •  BehavioralClam   ( @BehavioralClam@lemmy.ml ) 
    link
    fedilink
    arrow-up
    5
    ·
    4 months ago

    This is like a 10yo meme template, fellow kids pls update your meme stashes!

    •  sirdorius   ( @sirdorius@programming.dev ) 
      link
      fedilink
      arrow-up
      6
      ·
      4 months ago

    •  Pulptastic   ( @Pulptastic@midwest.social ) 
      link
      fedilink
      English
      arrow-up
      1
      ·
      4 months ago

      But why is it mirrored? Shiny is on the left, always has been.

      •  sunshine   ( @sunshine@lemmy.ml ) OP
        link
        fedilink
        arrow-up
        1
        ·
        4 months ago

        because I wanted the “punchline” to come at the end of the meme so I reversed it.

  •  Gamma   ( @GammaGames@beehaw.org ) 
    link
    fedilink
    English
    arrow-up
    5
    ·
    4 months ago

    Does that work? Might be enough to convert me

    •  skisnow   ( @skisnow@lemmy.ca ) 
      link
      fedilink
      English
      arrow-up
      13
      ·
      4 months 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   ( @GammaGames@beehaw.org ) 
        link
        fedilink
        English
        arrow-up
        5
        ·
        4 months 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   ( @sunshine@lemmy.ml ) OP
      link
      fedilink
      arrow-up
      6
      ·
      4 months 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.

  •  HiddenLayer555   ( @HiddenLayer555@lemmy.ml ) 
    link
    fedilink
    English
    arrow-up
    2
    ·
    4 months ago

    How is this implemented? Is it just functions and the language assumes the first parameter is autofilled with variable.function syntax?

    •  vga   ( @vga@sopuli.xyz ) 
      link
      fedilink
      arrow-up
      3
      ·
      4 months ago

      Ruby is object-oriented, modelled after Smalltalk mostly. So

      irb(main):001:0> 10.class
      => Integer
      

      So you’ll just have implement the method “years” on the Integer (or something more generic like Numeric) class and then “ago” on whatever class the years method returned.

      You might imagine that you can do something like 10.years().ago() in python but the parser prevents you:

      >>> 10.years
        File "<python-input-0>", line 1
          10.years
            ^
      SyntaxError: invalid decimal literal
      

      Doesn’t seem like it would have to prevent it, back in ruby:

      irb(main):001:0> 10.0.class
      => Float
      

      Ruby is a pretty cute language in my opinion, and I find it sad that python kinda drove over it.

      •  Oriel Jutty :hhHHHAAAH:   ( @barubary@infosec.exchange ) 
        link
        fedilink
        arrow-up
        1
        ·
        4 months ago

        That’s just syntax.

        >>> 10 .yearsTraceback (most recent call last):  File "<stdin>", line 1, in <module>AttributeError: 'int' object has no attribute 'years'
        
        •  vga   ( @vga@sopuli.xyz ) 
          link
          fedilink
          arrow-up
          1
          ·
          4 months ago

          Yeah, I figured there would be a workaround. Also

          >>> (10).years()
          Traceback (most recent call last):
            File "<python-input-0>", line 1, in <module>
              (10).years()
              ^^^^^^^^^^
          AttributeError: 'int' object has no attribute 'years'
          

          But the other thing is also: can you add methods to the int class so that they’re available everywhere? I suspect that you cannot in python, at least without significant hackery. And I also suspect that it’s probably something they decided to prevent knowingly.

  •  The Menemen   ( @menemen@lemmy.ml ) 
    link
    fedilink
    arrow-up
    1
    ·
    4 months ago

    “365*10”???

Programmer Humor@lemmy.ml

programmerhumor@lemmy.ml

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: !programmerhumor@lemmy.ml

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 65 users / day
  • 296 users / week
  • 745 users / month
  • 2.96K users / 6 months
  • 867 local subscribers
  • 39.6K subscribers
  • 1.1K Posts
  • 9.89K Comments
  • Modlog
  • mods:
  •  AgreeableLandscape   ( @AgreeableLandscape@lemmy.ml ) 
  •  cat_programmer   ( @cat_programmer@lemmy.ml ) 
  • BE: 0.19.13
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code