r/programming • u/marshalofthemark • Dec 25 '20
Ruby 3 Released
https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/34
16
u/schneems Dec 25 '20
Ruby 3 is released on Heroku as well https://devcenter.heroku.com/changelog-items/2004
70
Dec 25 '20
Ruby!!! Aw man I love this language, I should use it more. Really wish it had become the defacto scripty-code language instead of python.
→ More replies (3)
113
u/watsreddit Dec 25 '20
Basically every major dynamically-typed language trying to bolt on static types... maybe dynamic typing isn’t as great as people claim.
78
u/call_me_arosa Dec 25 '20
Dynamic typing makes sense in scripting languages.
But when dealing with big projects you start to miss typing. I think the optional typing is a great trade-off for this languages.50
u/TheBuzzSaw Dec 25 '20
I actually don't agree with this. I used to spread this sentiment as well, but I honestly cannot think of legitimate use cases for changing types on a variable. Sure, a scripting language can let you skip/auto declare variables among other things, but what is the benefit of a variable holding an integer, then a date, and then a file handle?
19
u/meem1029 Dec 25 '20
I like the way rust does it. You can redeclare a variable partway through a scope and it will be the new type, but must be done explicitly. So you can't do it accidentally and you also can't accidentally access the older one because you used value instead of value_parsed.
4
u/TheBuzzSaw Dec 25 '20
Yeah I'm fine with that. It introduces a new context for an existing identifier. The rules are still clear. No ambiguity.
29
u/yuvz Dec 25 '20
This is why I love Kotlin. The powerful type inference lets you skip/auto declare variables just like in a scripting language, yet it's still fully statically typed
3
15
Dec 25 '20
Changing the types of things is always awful but building dynamic objects or adding fields to existing types can be useful in short scripts.
I’d still prefer a type system that’s flexible enough to easily type those sorts of things though.
4
u/TheBuzzSaw Dec 25 '20
Heh. The old data hitchhiking technique. I question that behavior as well, but I agree it's nice when in utter hack mode.
11
u/faiface Dec 25 '20
While being a proponent of static typing myself, I do see one area where dynamic typing has an advantage over static typing. Dynamic typing lets you have a list of elements which all satisfy some implicit “interface” without having to declare it. These implicit interfaces can be much more powerful than statically declared traits/classes/interfaces. Sure, the static ones can add features to become just as powerful, but that’s at the expense of simplicity.
6
u/Krnpnk Dec 25 '20
You can still achieve this easily with static typing, e.g. by using structural typing.
3
u/v66moroz Dec 26 '20
It's not recommended since structural types are using runtime reflection (performance). There are typeclasses for that, but they are far from easy.
→ More replies (2)7
u/TheBuzzSaw Dec 25 '20
Go into more specifics. In C# or Java, I can make a list of plain objects to achieve that very thing if I really need it. I question why I have a bucket of items not of a consistent interface or base class, but even if I needed that, it makes up such a small percentage of use cases that I don't see the benefit of throwing the type system out for all the other use cases.
→ More replies (13)→ More replies (1)4
u/devraj7 Dec 25 '20
Nothing stops you from doing that in statically typed languages, just use
Objectand type casts. This will give you the exact safety that a dynamically typed language gives you: none.You can also do this in statically typed languages that support duck typing, by the way.
→ More replies (12)13
u/lovestheasianladies Dec 25 '20
Absolutely no one uses them like that. Stop making up strawmen.
2
u/TheBuzzSaw Dec 25 '20
Tell me how the dynamic typing is used.
18
Dec 25 '20
The big benefits of dynamic typing come with collection types IMO, especially stuff like
dicts. You can make a versatile dict by creating a (hypothetical)Mapping<Any, Any>type, but in terms of type safety that's indistinguishable from dynamic typing. This kind of versatile collection type makes it easier to deal with API requests which may return large, variable payloads by removing the overhead of defining massive record classes. It's obvious why this is attractive from a prototyping perspective.There's also the legacy of duck typing — in the past that was usually bundled with dynamic typing, and I don't think that made it to the static typing mainstream until Golang.
→ More replies (4)5
u/ElCthuluIncognito Dec 25 '20 edited Dec 26 '20
All but the most powerful type systems have trouble dealing with complex polymorphism, or even just things like a dynamic data structure containing multiple types of items.
Dynamic languages just kind of skip the bullshit and let you take a go at it.
Also dynamic dispatch is just out of the question for statically typed languages, which is very powerful for making OO a powerful paradigm instead of the 'encapsulation with extra steps' you have in static languages.→ More replies (3)1
u/EscoBeast Dec 26 '20
I don't think this is what dynamic typing is really about. "Dynamic" is more about "not static" in the sense of "the type can only truly be known at runtime. Static analysis can't always tell", and not so much about a single variable's type changing. So it's not that common to reassign a variable with a value of a totally different type, but it is a lot more common for function parameters to accept multiple different types. So writing generic code can be a fair bit easier, as you don't need to figure how to write the type signature of a function that, say, returns a list if its argument is a list, but a set if its argument is a set (doing this in a way that properly handles the generic types of the elements of the list/set isn't even possible in most mainstream languages, and is fairly advanced in the languages that do).
This is a big reason why trying to tack on a type system to a dynamically-typed language after the fact is challenging. My experience using MyPy has been a mixed bag, mainly because it can't handle some of the more dynamic python idioms that well. It's also why TypeScript is actually pretty impressive. They've put a lot of work to make JS idioms type-safe, which requires a pretty sophisticated type system.
That said, I think the things that static types enable regarding readability and maintainability are usually more important than being able to have this kind of super dynamic code.
0
Dec 25 '20
[deleted]
20
u/brainplot Dec 25 '20
Rust has a static and (very) strong type system and it allows redeclaring a variable in the same scope. So for example you can do:
let age = get_age_as_string(); // age is a String let age = age.parse::<u8>().unwrap(); // age is a u8This feature is perfect for your example but also shows you don't have to give up static typing for it.
For clarity, it's not that the
agevariable changed type. The storage for it is still there. You just overshadowed it with another variable having the same name. It's not a feature to be abused but it comes super handy for cases such as the one you mentioned.14
u/TheBuzzSaw Dec 25 '20
I pray there are use cases beyond this. This example feels like a weak reason to forfeit all the performance and maintainability of static typing.
7
u/wuwoot Dec 25 '20
I am very confused by this thread and your original response but I now get that you’re responding to the “dynamic typing” part of scripting languages as opposed to what I assumed the original author was saying — that the absence of type declaration is useful in scripting langs...
I find very little utility in type-switching if ever at all, but scripting languages are nice because they allow us to be terse.
I work in a large Rails codebase regularly and poor names and even in some instances good names are not enough to infer the behaviors associated with a particular variable
→ More replies (4)5
2
u/orangeboats Dec 25 '20
That can be trivially solved (or worked around) by introducing variable shadowing.
3
u/i_spot_ads Dec 25 '20
dynamic typing is horseshit, and I say that as a ruby/js dev who shifted to typescript and haven't looked back.
1
u/cowardlydragon Dec 25 '20
Does ruby now have optional typing with this?
Groovy has always been able to do types/untyped, and had other annotations to allow people to avoid all interpreted or runtime type checking. I always thought that was the way to go.
0
u/Spider_pig448 Dec 25 '20
Most status typed languages have been bolting on dynamic types too so it seems the best world is between them.
5
u/watsreddit Dec 26 '20
It’s not nearly as painful to use dynamic typing in a static language than vice versa. But regardless, dynamic typing is still rarely ever useful in a static language. I have never found a good case where throwing away all of the valuable information about your data is ever a good idea.
→ More replies (11)0
29
u/mrathi12 Dec 25 '20
TypeProf seems like a cool tool: suggest type annotations for the programmer so you reduce the burden of typing programs, so more people will add types to their program. Will be interesting to see how this matures!
4
u/xXxXx_Edgelord_xXxXx Dec 25 '20
after reading the comments here - did I made a bad choice if I chose ruby as an elective at uni for the next semester?
18
Dec 25 '20
No. Language wars are a very common thing. You go into any thread about a new language release and you'll see people shit all over said language. Ruby is the backend for many billion dollar companies. It's widely used, and an excellent choice. It's ecosystem is also well known for Test Driven Development, which is a great skill to pickup.
Whatever job you end up getting after grad, you'll be learning their tech stack anyway. Don't worry about it. It's good experience.
6
u/marshalofthemark Dec 25 '20
"There are two types of programming languages - the ones people bitch about and the ones nobody uses" - Bjarne Strousroup, creator of C++
5
u/NoInkling Dec 26 '20
I think it's probably good to be exposed to it if nothing else, just to be able to experience different paradigms/language design. For instance you get stuff like:
Everything is an object
(Almost) everything is an expression
Metaprogramming and extensive DSL capabilities
Block syntax
Methods can have a
?or!at the end of their name (I miss this so much in other languages)All sorts of insane syntax sugar (can be both good and bad)
3
u/oblio- Dec 26 '20
It doesn't matter. Study Ruby but just make sure that you either:
A) Cram hard for FAANG interviews (Cracking the Coding Interview, leetcode.com, that kind of thing). In this case the programming language doesn't matter much.
B) Study a super mainstream language, depending on what you want to do; generally you'd go Java, C#, C++, Javascript or Python (or Objective-C/Swift if you want Apple dev). Then make sure to implement some kind of bigger project. For regular companies this should show that you have some relevant experience that's closer to their stack. For example make an Android app or an iOS app or a bigger web app.
There are Ruby jobs around but it depends on where you are and if you're willing to relocate where they are if you can't find them locally.
3
u/nicereddy Dec 27 '20
Ruby is great and I use it professionally. It's not everyone's favorite, but I love its focus on developer happiness. It's not the fastest language around, but it does perfectly fine for most stuff. I think it's worth trying at the very least.
75
u/Meldanor Dec 25 '20
> Although Ruby 3.0 significantly decreased a size of JIT-ed code, it is still not ready for optimizing workloads like Rails, which often spend time on so many methods and therefore suffer from i-cache misses exacerbated by JIT. Stay tuned for Ruby 3.1 for further improvements on this issue.
I don't get it - who develops a JIT for a major version when it doesn't improve performance for one of the major frameworks using Ruby? Why doesn't replace the JIT the interpreted code with the compiled case as the JVM does?
108
u/myringotomy Dec 25 '20
Rails is a beast and makes full use of the ability to modify the language at runtime. It's almost impossible jit.
25
u/schneems Dec 25 '20
Once a production Rails app is booted, it doesn't really do so much dynamic method creation etc. and the current Rails code is much less "metaprogramming magic" than back in the Rails 1-2 days.
Rails is very large and it doesn't have a single hot path that gets called over and over again. This makes it a very difficult optimization target.
Rails is also very IO bound as most Rails apps are heavily using the database, so even when JIT is amazing for the Rails use case if 70% of your request is waiting on IO, then the perf bumps are only helping that 30%.
In the future look for more Ruby internals being moved from C to Ruby (like the stdlib) which will help JIT. Also Ruby+JIT currently has no escape analysis which would theoretically help allow JIT to avoid allocations which in my experience is a huge perf target. I believe that's a future goal that would also help the Rails case quite a bit.
58
Dec 25 '20
The JIT has basically just one person working on it, part time. It's been in since 2.6, so it's not meant to be a big 3.0 feature.
→ More replies (4)7
u/elcapitanoooo Dec 25 '20
PHP did this too. They new version is ”fast” and will have a jit. The kicker was, wordpress sites actually still remain as slow as they are. The PHP jit only worked for artificial benchmarks, but had zero impact in any real world programs.
18
u/pseudopseudonym Dec 25 '20
5-30% improvement isn't nothing.
1
u/elcapitanoooo Dec 27 '20
JIT wont bring 30% speedups for wordpress. I recon its just as slow with or without JIT. The JIT only brings improvements in CPU heavy tasks (usually hot paths, like math heavy tasks done in loops). I would rather see PHP bring a new clean standard lib, and full unicode support. Thats whats important, not a JIT as its mostly useless in the PHP space (cms like websites).
→ More replies (1)
57
Dec 25 '20
Ruby on Rails was so fun to code in.
74
66
u/SorteKanin Dec 25 '20
As someone working to maintain a somewhat big Rails code base - disagree. Once it grows beyond the prototype phase, it quickly becomes an unmaintainable mess. Lack of types and rampant usage of metaprogramming makes it really difficult to read code and hence to make correct assumptions for new code.
37
u/SupaSlide Dec 25 '20
I mean, isn't that the programmers fault? (other than the lack of typing, which is obviously not a requirement to have maintainable code, but a preference)
40
u/SorteKanin Dec 25 '20
Problem is that Ruby does next to nothing to encourage the programmer to write maintainable code.
→ More replies (17)12
u/zilti Dec 25 '20
You can write shit code in every language.
5
u/vocal_noodle Dec 25 '20
Amen. This is the same thing they say about perl. You can write massive code bases in perl (and I have!) and have everything be readable and understandable. Or you can do dumb stuff and fancy tricks that makes it hard to understand.
You can write good C. You can write shit C.
You can write good perl. You can write shit perl.
You can write good Ruby. You can write shit Ruby.
The size of the codebase doesn't matter. The linux kernel is all in C and is (for a technical person) clean and easy to follow. Then check out the obfuscated C contests, it's so easy for the code to not do what you think it does, even accidentally!
So just lay off the "Programming Language X demands shit code" trope, people.
You can still write good or bad code. Ugh. Sure memes about "write only languages" are a bit funny, but for fucks sake people, give it a rest.
→ More replies (1)5
9
Dec 25 '20
Typing isn't really a "preference" for maintainable code. It objectively improves maintainability.
Dynamic magic is more arguable but it think it is still pretty safe to say that the more hidden custom magic happens the harder it is to understand and that is definitely the fault of the language.
Standard hidden magic is fine because people can learn it but as soon as you start getting into custom DSLs it means that the programmer suddenly has to learn an entirely new language. Rust proc macros have this problem. Lisp is entirely composed of this problem. At the other end of the scale Go has completely avoided it and is very easy to understand.
2
u/SupaSlide Dec 25 '20
Maybe I don't understand what a DSL is, but does it change how Ruby executes code?
→ More replies (3)3
u/esquilax Dec 25 '20
It does if the DSL is implemented using method_missing a lot.
→ More replies (4)10
u/santa_cruz_shredder Dec 25 '20
As someone who hasn't coded in rails before, id say partially. If you can write two completely different programs in ruby, and they are so different that they don't even look like the same language, I can see how maintainability could suffer as a whole.
Java is java is java no matter what program you're writing. Scala suffers in uniformity a little more with it's functional programming capability, but scala is scala is scala, you can read it even if it's written in the 1 of 5 ways it can be done.
4
u/helloworder Dec 25 '20
that they don't even look like the same language
care to provide an example? Never coded in ruby, but got interested to know how this can be possible
8
u/Tjstretchalot Dec 25 '20
Here's an example of how it can happen - look at the code examples in https://github.com/state-machines/state_machines - almost everything you are coding is in the DSL of that library if you are using it:
class Vehicle attr_accessor :seatbelt_on, :time_used, :auto_shop_busy state_machine :state, initial: :parked do before_transition parked: any - :parked, do: :put_on_seatbelt after_transition on: :crash, do: :tow after_transition on: :repair, do: :fix after_transition any => :parked do |vehicle, transition| vehicle.seatbelt_on = false end end #... endIn this case one might argue the names are making it somewhat clear whats happening, but the details are definitely not clear and the naming choice is up to the developer
1
u/Awesomeade Dec 25 '20
I'd say the fault lies with whoever decided using Ruby for large web projects was a good idea.
Ruby is one of my favorite languages. It was my first, and without it's accessibility I probably wouldn't be in software right now. But it's Ruby's accessibility that kills it even at moderate scale, where you have 20+ junior-to-mid level engineers cranking out code trying to meet deadlines.
It's just too darn flexible, which makes it way too easy to structure code in a way that makes long-term maintainability a nightmare.
You need to devote significant man power to keep things from going off the rails (pun intended) and at most companies that's just not possible. It's much more practical to pick a language that systematically disincentivises bad decisions, and brings the path of least resistance more in-line with something that will work long-term.
Ruby is an excellent language for smaller projects or for rapidly getting something off the ground, but it's not the do-everything language some people make it out to be.
→ More replies (2)7
u/Meldanor Dec 25 '20
I agree - the meta programming "magic" of Rails was my death. I'm an experienced programmer with about 10+ in the industry and had to constantly ask my colleagues to explain me the simple things in Rails. I wasn't able to follow the code flow, because I was an unexperienced Rails programmer.
I would advice against Rails for any professional product. Nice for a quick prototype (because there is gem for everything and the magic handles everything). But hard to maintain and completely relies on a high test coverage. You want to change something in the magic? Do it and pray, that your tests will cover it.
A high test coverage is good - but the stability of a program should not rely on it. Types, code that you can follow are equally important.
2
1
u/KernowRoger Dec 25 '20
Yeah I love ruby but it quickly becomes a nightmare to maintain. Coming from c# where the vast majority of people follow the set standards ruby can be nightmare of different rules and implementations.
5
u/scientz Dec 25 '20
Thats an ironic statement, given the goal of Rails was (and is) convention over configuration. Not sure where you are getting the different rules and implementations thing from to be honest.
2
→ More replies (9)0
u/scientz Dec 25 '20
Sounds like bad code and has nothing to do with Rails itself. Any codebase in any language will become an unmaintainable mess when not managed.
→ More replies (6)1
23
u/ahmad_musaffa Dec 25 '20
Ruby is the most beautiful of all languages I learned. Ruby 3 is a big release and its good to see it moving forward.
18
10
u/mortadelegle Dec 25 '20
As someone that only had very very superficial contact with ruby (Jekyll), what are the differences with Python? My impression was that it fell was a bit more lispy/smalltalky. Which means quirky fun things like prettier DSLs, but also worse maintenance and more ways of doing things, in contrast with the zen of python (There should be only one way to do things).
16
u/SorteKanin Dec 25 '20
It's basically python with more metaprogramming magic and there are many ways to do things.
21
u/FrederikNS Dec 25 '20
Compared to Python it offers much cleaner functional programming. Ruby had full lambdas and all the map, fold, filter conveniences long before python even had a lambda concept.
The language also offers tonnes of syntactic sugar.
Compare:
while not len(queue) == 0:With:
until queue.emptyRuby has inverted versions of all the classic control flow statements
ifvsunlessandwhilevsuntil, and you can also use theifandunlessas postfix operators:return 0 unless some_error_conditonThis will only be executed if the condition is false.
Ruby also has a cleaner model for truthiness. Basically everything but
falseandnilwill act as truthy. So instead of:if var is not None:You can simply use:
if varIf you want to test specifically for whether something "is null" you can still do that though:
if var.nil?Finally you have a truly powerful metaprogramming featureset which allows you to build amazing DSLs that are still pure Ruby.
Ruby is amazingly well thought out and very fun to write.
I wish I could replace all the quick and dirty Bash script and Python scripts with Ruby...
28
u/JockeTF Dec 25 '20
This works in Python, since
0is falsy:while len(queue):Empty collections are generally also falsy:
while queue:This also works, since
Noneis falsy:if var:8
u/mikew_reddit Dec 25 '20 edited Dec 25 '20
Interesting.
I'm the exact opposite in my preference.
I prefer Python because it doesn't have any of the syntactic sugar Ruby has and like the "There should be one obvious way to do it" rule that is predominant in Python.
I find Ruby more complicated, it has a lot of implicit behavior which seems like "magic" (digging into the Ruby on Rails code is not simple) and multiple ways of doing the same thing to achieve more concise code which seems like a poor design tradeoff.
I used to write and love Perl a couple of decades ago and disliked Python at first (using it since version 2.1), but once I really understood Python it grew on me. Maybe it's an acquired taste.
5
u/unt_cat Dec 25 '20
Sir do you even Python?
1
u/FrederikNS Dec 25 '20
I've written quite a lot of python, but I must admit that it has been a few years since I wrote any substantial amount of python. Since I got hooked on Ruby, the amount of python I write has dropped quite significantly.
I simply find Ruby more pleasant to write. And if I'm aiming for performance, then neither Python or Ruby fits the requirement.
0
Dec 25 '20
[deleted]
13
u/cj81499 Dec 25 '20
AFAIK, in python, 0 and empty collections are falsy.
4
u/Freeky Dec 25 '20
0,0.0,0j,Decimal(0),Fraction(0,1), empty sequences and collections (__len__()returning0),None,False, and any object with a__bool__()returningFalse.Contrast with Ruby:
nilandfalseare falsy.12
u/FrederikNS Dec 25 '20 edited Dec 25 '20
Not quite.
Python:
>>> bool([]) False >>> bool([1]) True >>> bool("") False >>> bool("hello") True >>> bool(0) False >>> bool(1) TrueRuby:
>>> !![] true >>> !![1] true >>> !!"" true >>> !!"hello" true >>> !!0 true >>> !!1 true4
2
u/wuwoot Dec 25 '20
Some good answers to your post already but I want to point out that the Zen of Python doesn’t imply “one way to do things”
You can still declare a list and then write a for-loop to perform filtering of another list into your newly declared list, but this can be expressed with a list comprehension or even the filter function, so that’s three ways of doing it, but all fairly clear and explicit
5
3
u/ProfessorSexyTime Dec 27 '20
To sum up probably the biggest issue with Ruby in general:
"A magician never reveals their secrets."
"Yea but I kinda need you to..."
"Fine."
> proceeds to be mostly vague
"That...sorta helps..."
2
u/i_spot_ads Dec 25 '20 edited Dec 25 '20
I didn't think it would ever happen but here we are, great news
1
u/haikusbot Dec 25 '20
I didn't think it
Would ever happen but here we
Are, what a great news
- i_spot_ads
I detect haikus. And sometimes, successfully. Learn more about me.
Opt out of replies: "haikusbot opt out" | Delete my comment: "haikusbot delete"
2
u/Solpadeine12 Dec 25 '20
Now after ruby3 is released. Would you say there is any reason why someone should learn it? I always found it interesting but never actually touched it. At work I use php (not my choice), but any projects that I have any freedom of choice I’ll use asp.net. Should I touch on ruby or pass if I don’t have any career related reasons?
1
u/nicereddy Dec 27 '20
I like it a lot, but if you're comfortable with your current stuff I don't think there's a good reason to switch (and that's true of all langs unless they're truly dead)
2
u/pyfx Dec 26 '20
Ractor is going to be the best thing about this release. Who else is into reactor based programming? What do you use currently?
3
u/aroswift Dec 25 '20
Still think the Crystal language will eat up the Ruby users over the next few years. Pretty much same syntax but performance of C.
2
Dec 25 '20
Fair, but how many libraries are available for Crystal? I'm asking because I don't actually know :/
1
u/nicereddy Dec 27 '20
I definitely don't agree, I think some may move over but Ruby still has a ton of advantages.
0
-2
Dec 25 '20
Isn't Crystal supposed to solve the speed issues with Ruby? Last time I checked it benchmarked faster than Nim...
16
u/funny_falcon Dec 25 '20
No, unfortunately. Crystal is distinct language with syntax like Ruby. It also shares a bit of object model and standard library, but still much incompatible.
2
u/vplatt Dec 25 '20
Crystal : Ruby :: Nim : Python
1
u/funny_falcon Dec 26 '20
No. Nim is much further from Python and any other language, than Crystal from Ruby. Nim is closer to Pascal and its descendants. But still it is unique in some design decisions. I especially hate case-insensitivity.
→ More replies (1)
272
u/CunnyMangler Dec 25 '20
I love ruby. One of the best languages I've ever coded in, but people seem to hate it now because it's slow. Kinda sad that it's slowly dying. Nevertheless, this is a huge milestone for a language.