Golang for Rubyists. Part 7. Ruby and Golang, methods comparison
Hello, my dear friends. We all love Ruby (you too, right?) for its expressiveness and a set of useful methods out of the box. It would be a pleasure if when start using a new language, you had the similar methods or at least familiar ways to achieve same goals. Let’s try to pick a few useful methods from Ruby and find out, are there any equivalents in Golang for them.
Array
Let’s start with something simple:
So, we used four methods here: shift, unshift, push, pop
. Now let me try to do something similar in Golang:
https://play.golang.org/p/wNgO9LeX514
Not so short and expressive, huh? But still pretty straightforward. The only confusion I can anticipate is the last example, with pop and unshift. Just to explain, at first we assign to x
the last element of an array
and we modify the array
variable to be everything starting from the element at index 0 to the pre-last one. And at the next line we create a new slice with just x
and append an array
to it.
Also, you could notice here, that usage of []
to access an array/slice element is exactly the same as in Ruby (or most of the languages).
Let’s move on to the more solid examples:
Here we iterate through a collection, without original collection modification, then we reject
some value also without a modification, then we get a random element from it, then we have a basic min/max methods and a shuffle
method, which shuffles an array elements. Are you optimistic about Golang abilities here? Let it a try! Nope, let me just tell you, that Go is not a functional language. Not at all. Not for an iota. The only way you can achieve map/reduce/filter functionality is by writing for
loops. Also, there are no built-in min/max functions, unfortunately. Surprisingly I was able to find a shuffle
implementation:
Btw it still uses two for
loops under the hood: https://golang.org/src/math/rand/rand.go#L232. Interesting to notice here is that it’s possible to pass a function to a method in Golang.
Just to give an impression, here is the possible implementation of our own Filter
function in Golang:
https://play.golang.org/p/LFvueXzO-BU Here is a version, using interfaces, more generic, but still far from ideal https://play.golang.org/p/N9JvUXo7ugq
As you can see, this will work only with slices of integers. In the link above you can find a more generic implementation. It uses reflection mechanism of Golang, which we didn’t explore yet.
So, this function may look cumbersome, but let’s take a look onto Ruby’s map
method under the hood:
Looks pretty similar, isn’t it? Sure, in Ruby we don’t work with types - that’s the only difference. So, long story short, use for
loops, that’s the way to go.
But also one thing I want to mention is that there is a more convenient way to iterate through a collection, here how it looks like (works for both maps and arrays):
(https://play.golang.org/p/6g28EUCvQ6f)
Hashes
One of the most heavily used data structures in Ruby is Hash. Just kiddin, I have no idea, which is the most heavily used one. Let’s take a look at these examples, where we apply a couple of useful functions to it:
Nothing magical, just retrieving all values, all keys, and values by multiple keys, should be easy-peasy?
(https://play.golang.org/p/YN0xnhly-r9)
I am honestly not sure about the last one, probably there is a way to make it more readable. But anyway, beauty of Go in it’s straightforwardness :)
String
In Ruby, Strings can operate as arrays in most cases, but here we will pick a few string-specific methods:
Nothing wow-like for Ruby, boring stuff, but let’s check, what Golang has to offer for such basic things:
(https://play.golang.org/p/9GvLs9yFIea) Bang-bang! Such a bliss! Actually, Golang has a pretty extensive set of methods for strings manipulation, you can find way more in their official documentation: https://golang.org/pkg/strings/
Conclusion
There are much more to discover, so start using Golang for your pet projects or for your actual projects at work, f.e. if you plan to have some simple Lambda function, it’s a good place to introduce it, check out my past blog post on how to make it painlessly. But for today that’s it, thank you for reading. If you don’t want to miss the next posts, subscribe to my Twitter, tho I chat there for random topics, also I share some interesting posts I’ve discovered, as well as my own. And psst, there is a big orange button beneath, using it, you can buy me an espresso macchiato, which I really love and will appreciate ;) P.S. Thanks Claude for coffee and pleasant feedback, it helps staying motivated!