News for hackers. Stuff that matters.
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by matheusmoreira on Sunday January 19, 2020 @08:52AM (#22091418)
Ruby could use a proper module system as well. Ruby's require evaluates Ruby code and changes global interpreter state. Python's modules are so much better. Javascript's require is so simple: a function that returns a module object.
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by BurningFrog on Sunday January 19, 2020 @11:24AM (#22092201)
Yeah, having gone from Ruby to Python, it's a downgrade in most ways. Clumsy, ugly language.

But, man, the Python import system is so vastly superior to Ruby's "invisible distributed magic" way of handling the same thing.

The sad part is that there is no reason a good language could't have the good parts of both languages. But here we are...

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by matheusmoreira on Monday January 20, 2020 @10:48AM (#22099616)
> having gone from Ruby to Python, it's a downgrade in most ways. Clumsy, ugly language.

Python 3 is a much better language than Python 2. More consistent and much better designed.

I miss the Ruby standard library though. All those string, array and hash methods make things so easy. I often get stuck looking for Python equivalents.

> The sad part is that there is no reason a good language could't have the good parts of both languages.

There is: backwards compatibility. Most of the time it's impossible to fix a language because the result would be two different languages that have the same name. Python broke compatibility with version 3 and it is still competing with version 2 to this day. Ruby also did the same thing in version 1.9 but thankfully the scale was much smaller.

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by jeltz on Sunday January 19, 2020 @09:07AM (#22091485)
As someone who codes in Ruby daily this is my by far biggest issue. Ruby's performance is pretty good (I do not use Rails or ActiveRecord) and while the state of concurrency is not ideal it is not really an issue in practice for me, but the lack of a proper module system forces me to invent unnecessarily long and convoluted names.
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by vidarh on Sunday January 19, 2020 @09:54AM (#22091707)
That would be a very jarring change. Ruby class definitions are executed to define a class. There is no way to statically define a class in Ruby. This is frequently used. E.g lots of Ruby libraries add class methods that are executed during the definition of a class to mutate it during definition. This is Ruby's alternative to macros.

As such, you'd gut large parts of the language if you stopped evaluating code on require.

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by matheusmoreira on Sunday January 19, 2020 @11:21PM (#22096271)
The code evaluation is not the problem. The fact all evaluation takes place in a single global interpreter context is the problem. When a file that defines a class A is required, the A constant of the Object class gets set to a new instance of Class:

  # a.rb
  class A
  end

  # irb
  require 'a'

  Object::A.new
All code evaluation modifies global interpreter state. Honestly it's not much better than the C preprocessor's #include directive.

A better design: files are always evaluated with a clean interpreter state and they can return values just like methods.

  # a.rb
  class A
  end

  A

  # irb
  a = require 'a'

  a.new
This makes all state local to each script by default. Users will only have access to the returned value which can contain anything or nothing. If a module needs to modify existing classes, they could return modules that can be mixed in or metaprogramming methods that perform the modifications when called with the targeted class as parameter. Perhaps existing classes could even be passed as parameter:

  require 'extensions', Object, Kernel
Of course, these changes would break compatibility with all existing gems. The result would be a completely different language that happens to also be called Ruby.
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by vidarh on Tuesday January 21, 2020 @05:58AM (#22105836)
Most of the code that changes global interpreter state does so in predictable ways that are perfectly fine, so I don't think what you're suggesting would actually make much change.

Most modern Ruby code avoids monkey-patching "unasked" unless that is an explicit purpose of the project and using the gem is explicitly "asking for it" in the first place. Providing modules to include or making you explicitly call methods or explicitly require files documented to do optional monkey-patching has been the idiomatic way of handling it for years.

Adding the ability to explicitly provide guarantees that the required code is sandboxed might be useful and might particularly help tooling reason better about the code, but it would have quite little practical effect on most modern Ruby.

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by pdimitar on Sunday January 19, 2020 @12:46PM (#22092700)
Yep, but that's a post factum worry. It's not an inherent trait of being a dynamic language that automatically surfaces during its development. Ruby's creator(s) simply chose to do it like that and now, of course, it's too late to do any sort of revolutionary changes.

Hi btw! Just now noticed it's you. :) Was thinking to write you an e-mail very soon. Which one do you prefer?

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by vidarh on Tuesday January 21, 2020 @03:55AM (#22105318)
Yes, of course it could have been done differently from the start, but as you say it's now impossible to change. Though my Ruby compiler project (that seems like it will take the rest of my life at this point...) would have been far easier if it was more static. That said, I think it's less of a problem in practice - most code does things that can be fairly easily statically determined and/or that isn't actually doing anything particularly nefarious to other classes. A lot of the worst abuses that are possible with Ruby are also things I've never seen done intentionally in the wild other than as examples, and where deprecating and explicitly breaking them wouldn't do any harm.

> Hi btw! Just now noticed it's you. :) Was thinking to write you an e-mail very soon. Which one do you prefer?

Sure :) Same old e-mails (also on my HN profile page).

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by bernawil on Sunday January 19, 2020 @01:54PM (#22093139)
Isn't the real problem that Ruby's "magic" a lot of the time relies on monkey-patching stuff?

I mean, Javascript's modules also are dynamically executed and then returned as a singleton module object (also cached, so only the first require actually runs the code before the module export). This means you can make a module which requires other modules and monkey patches methods inside them (this is how mocking actually works), wrecking havoc amongst all other dependencies, but library authors just don't.

Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by vidarh on Tuesday January 21, 2020 @03:41AM (#22105268)
The more in-your-face monkey-patching went out of favour many years ago. Most newer Ruby libraries make you explicitly "opt in" to changes by e.g. including a module and/or contain any monkey-patching in separate files that needs to be explicitly require'd if you want that behavior. It's rarely a problem in practice.
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by pansa on Sunday January 19, 2020 @12:58PM (#22092779)
Doesn’t Python’s `import` evaluate code in the same way as Ruby’s `require`?
Re:Yukihiro Matsumoto: “Ruby is designed for humans, not mac... by wanderer2323 on Sunday January 19, 2020 @10:06PM (#22095952)
I think so, here is the article about it:

https://www.benkuhn.net/importtime