Live data from Hacker News

Happy Birthday, Ruby

github.com

181–190 of 239 posts

Re: Happy Birthday, Ruby

#181

Earlier quoted context omitted.

(def fizzbuzz-nums (range 1 101)) (defn fizz? [n] (zero? (% n))) (defn buzz? [n] (zero? (% n 5))) (defn fizzbuzz? [n] (and (fizz? n) (buzz? n))) (defn fizzbuzz [n] (cond (fizzbuzz? n) "FizzBuzz" (fizz? n) "Fizz" (buzz? n) "Buzz" :else n))) (def fizzbuzz-list (map fizzbuzz fizzbuzz-nums)) (apply println fizzbuzz-list)

Simple. But this brings up a debate I've been having with some folks. I have the memory of a gnat, so I can't keep a lot of context in my head. I prefer the previous two examples to this one, simply because with this one, I have to remember a lot more contextual vocabulary. It's a pet-peeve of mine to pull up a source file, and then have to ping-pong around between 50 different function calls, when the entire thing c…

Well, I would say that the primary refactoring done in Slackwise's example is just to assign names to the different conditions and the list produced by range. I think it makes most sense to break these things out and assign them to names when they both become a common idiom in your code (it is used in multiple places) and the logic spans a few lines or has sufficient complexity. I, too, would rather look at one piece of code than have to find out what each function is attempting to do if the name is perhaps ambiguous. For Slackwise's example, I would say that that is too much abstraction because you've basically created as many pieces of abstraction (the conditionals and list) as there were lines of code without them. Maybe the decision for when to abstract can boil down to:

* Is a common idiom in the code base / same logic used in multiple places

* Has "sufficient" complexity

* Can be replaced with a good name that is generally clear with what it will achieve

For Slackwise's example, I think that the names used in the abstractions are exactly what I would use, but I also think that they're not really descriptive enough. If I wasn't the original author and thought I should refactor the names, I would probably try to choose divisible-by-3?, divisible-by-5?, and divisible-by-15? because what the hell is a fizz or a buzz anyway? Maybe you had already thought about all of this and wanted a deeper response; sorry to disappoint.

Re: Happy Birthday, Ruby

#182

Earlier quoted context omitted.

So, I'm old, and have done lots of stuff in lots of languages in frameworks. For about a dozen years, Rails was my go-to toolkit. I've written a couple dozen production applications with it. Unfortunately, Rails has really fallen out of favor lately. Even a Rails-specialty shop I worked for briefly has pivoted to using ASP.NET. I've played around in that stack, and found it lacking. (EF just doesn't compete as an ORM…

So...eh. Ruby has the easiest data access tools, between ActiveRecord and Sequel (which I prefer), out there. I don't know too many folks who'd disagree with you about that. But where you're happy with Rails taking a few lines, I am as of late big on Kotlin (because I agree with you regarding Java) encouraging me to be correct . I'm building a new product using Spring Boot, Kotlin, and JDBI and while, yes, there's bo…

> Ruby has the easiest data access tools, between ActiveRecord and Sequel

Not true

Re: Happy Birthday, Ruby

#183

I'm currently working on a Ruby project, and I love the language so much! It's elegant, fast enough for me, and the ecosystem rocks. It seems that, after Ruby borrowed from many previous languages, a lot of rubyisms went into Rust (the nice functional-style enumerations, the awesome package manager inspired by Bundler). And I'm thrilled the best of the Ruby world found its way into other languages. A future where the…

Wouldn't Crystal fit the bill for a fast compiled language explicitly designed to emulate Ruby's syntax? (I don't have any significant Ruby or Crystal experience, so if there are reasons why that doesn't fit the bill, I'm sorry)

For me, Elixir fits the bill as a Ruby replacement better than Crystal does. Elixir is more mature and its community and tools are similar to Ruby, making the upgrade path easier, I think. At least that was the case for me after trying both. I also have come to love the functional aspect of Elixir over Crystal's (and Ruby's) focus on OOP.

Re: Happy Birthday, Ruby

#184

Earlier quoted context omitted.

Elixir/Unix style pipe operations in Ruby! https://github.com/LendingHome/pipe_operator

it's not nearly the same. Piping in elixir is a macro; it lets you preserve the other arguments in your function call. The IO.inspect(value, label: "label string") semantic is incredibly powerful. During debugging I often do this: value |> IO.inspect(label: "A") |> do_something |> IO.inspect(label: "B") |> do_something_else |> IO.inspect(label: "C") ... This gives me full visibility over the data transformations that…

That's what it's doing, arguments are preserved:

    # don't really know what IO.inspect is doing
    # but guessing something like this?
    def IO.inspect(value, label:)
      puts "#{label}: #{value.inspect}"
      value
    end

    value.pipe do
      IO.inspect(label: "A")
      do_something
      IO.inspect(label: "B")
      do_something_else
      IO.inspect(label: "C")
    end

Re: Happy Birthday, Ruby

#185

Earlier quoted context omitted.

#!/usr/bin/env ruby puts((1..100).map do |n| "#{:Fizz if (n % 3).zero?}"\ "#{:Buzz if (n % 5).zero?}" .then .find { |s| !s.empty? } || n end)

I'm curious: do you genuinely find that beautiful?To me all the enumerable/map/frozen string literal stuff seems pointlessly overcomplicated compared to the simplicity of just writing down what you're supposed to do: (1..100).each do |n| a = String.new a (Disclaimer: not my code, on mobile so I stole that off GitHub)

Not really; I was basing it off of https://news.ycombinator.com/item?id=18873581#18875961 as a semi-joke.

I changed it to a relatively KISS and IMO genuinely beautiful version in the parent, for others' benefits here's the version you were referring to in the above comment:

  #!/usr/bin/env ruby
  # frozen_string_literal: true

  module FizzBuzz
    Integer.include self

    def self.array(enumerable = 1..100)
      enumerable.map(&:to_fizzbuzz)
    end

    def to_fizzbuzz
      "#{:Fizz if (self % 3).zero?}"\
      "#{:Buzz if (self % 5).zero?}"
        .then
        .find { |s| !s.empty? } || to_s
    end
  end

  puts FizzBuzz.array

Re: Happy Birthday, Ruby

#186

Earlier quoted context omitted.

I'm not sure what language would be more beautiful than ruby, to be honest, and it's not even my favorite language to use. Let's do a mini code challenge in the thread -- implement fizzbuzz in the most beautiful way you can, in the most beautiful language you know:

> I'm not sure what language would be more beautiful than ruby. Elixir (with static type analysis) - gets a bit of an edge, IMO, thanks to pipes, which are way prettier than .then: defmodule FizzBuzz do @spec fizzbuzz(integer)::String.t def fizzbuzz(int) do cond do rem(int, 15) == 0 -> "FizzBuzz" rem(int, 5) == 0 -> "Fizz" rem(int, 3) == 0 -> "Buzz" true -> inspect int end end end 1..100 |> Enum.map(&FizzBuzz.fizzbuz…

as per Jpakotal's comment elsewhere this might be even prettier:

    1..100
    |> Enum.map_join("\n", fn 
      int when rem(int, 15) == 0 -> "FizzBuzz"
      int when rem(int, 5)  == 0 -> "Fizz"
      int when rem(int, 3)  == 0 -> "Buzz"
      int                        -> inspect int
    end)
    |> IO.puts

Re: Happy Birthday, Ruby

#187
post #122
post #93

Earlier quoted context omitted.

Your complaints regarding ruby has been noted. It's not you favorite language and that's cool. Every language out there has pros and cons with different set of tradeoffs , you can't point at a language and objectively say X is better than Y. Ruby has its place and whilst you think Rails is "magic" the internet begs to differ. Of course any framework looks like "magic" if you don't bother digging into implementation d…

"Ruby has its place and whilst you think Rails is "magic" the internet begs to differ. Of course any framework looks like "magic" if you don't bother digging into implementation details of it. That is the whole point!" I think most people including many Rails proponents would agree that Rails has a huge emphasis on "magic" compared to other frameworks. It's a feature. Convention over Configuration implies magic and t…

Magic or abstraction layers should be intuitive, so you understand them and are able to solve edge cases. If the abstraction layers are too foggy or magical and you are unable to fix edge cases yourself, something is wrong.

Re: Happy Birthday, Ruby

#188
post #43
post #16

Ruby is the very least favorite of all the programming languages I've had to use regularly over my career. Its syntax strongly favors cuteness over familiarity. Wherever Ruby can diverge from expectations to give you a pointless little tickle of whimsical inventiveness instead, it seems to do so. Visually it looks like an unwanted love child of Pascal and Python. There's no clear rhyme or reason to the use of sigils…

I wholeheartedly agree. My main gripe is that people insist Ruby is 'easy', because it 'looks just like English'. Yeah, well, it doesn't. Programming languages are different from normal languages because they have different goals, different users and different requirements. The result in of this forced unification in Rails is something that resembles English, if you squint really hard, but in order to do so, it uses…

Fully agree.

Re: Happy Birthday, Ruby

#189

The decision to implement DSLs by overriding method_missing seems to have opened a can of worms and the community has never managed to put the lid back on. More than anything else, this is the thing that seems to cause no end of consternation with Rails. At the same time, it's pretty much the defining feature of Rails.

I'm not aware that this has caused much consternation. I disagree that DSLs are a defining feature of Rails. There are other ways to implement DSLs besides `method_missing`, and rubocop and other style guides warn against using this method.

I'll admit to not having used Rails, but I was under the impression that Rails itself was a DSL.

Re: Happy Birthday, Ruby

#190
post #113

Earlier quoted context omitted.

Ruby the Ruby way: module FizzBuzz def to_fb s = '' s

Cleaner and more idiomatic version: https://news.ycombinator.com/item?id=18873581#18877403

IMO monkey-patching built-in classes is idiomatic Ruby!

Edit to add: Actually, now that I think about it, using method_missing would have been even better!

Post reply on HN