Great article about pros and cons of Elixir, in comparison to Ruby. https://www.netguru.co/blog/elixir-vs-ruby-which-one-is-the-language-for-your-next-project and how to choose the right one depends on your next project.
Reading well-written Ruby code is also a pleasure.
Isnāt this the case for about any language? A good question is how often does the āwell-writtenā code pop up?
Ruby on Rails, created back in 2005, was a really disruptive piece of technology. It changed how we write web applications - it was just mind blowing how much you could achieve by writing so little code. It also made prototyping applications faster.
Iām guessing they werenāt Perl users. ^.^
Ruby is a really expressive language. A couple of lines in Ruby can do the same job as whole chunks of code in other languages.
Iāveā¦ not actually seen this yet for Ruby. I keep hearing that such-n-such languages do this, but it is surprising how often they some how end up more verbose than ābig-fastā languages such as C++ or OCamlā¦ Anyone have some code that examples this that also fulfills the well-written part (please no perl-golfing horrors)?
Also, if they are really going to say FP is a Con for Elixir, then they really badly need to say that OOP is a Con for Ruby. ^.^;
PHP
class Ball
{
private $_colour;
public function __construct($colour)
{
$this->_colour = $colour;
}
public function is_red()
{
return ($this->_colour == 'red' ? true : false);
}
}
$the_ball = new Ball('green');
echo ($the_ball->is_red() ? '' : 'The ball is not red');
Ruby
class Ball
def initialize(colour)
@colour = colour
end
def is_red?
@colour == "red"
end
end
the_ball = Ball.new("green")
puts "The ball is not red" unless the_ball.is_red?
Can even be written like this:
class Ball
def initialize(colour); @colour = colour; end
def is_red?; @colour == "red"; end
end
the_ball = Ball.new("green")
puts "The ball is not red" unless the_ball.is_red?
Or this:
class Ball
def initialize colour; @colour = colour; end
def is_red?; @colour == "red"; end
end
the_ball = Ball.new "green"
puts "The ball is not red" unless the_ball.is_red?
I donāt think your example shows that ruby gains a lot compared to php. The php and ruby are almost exactly the same when written in the full format. Only small syntactic differences. And for your shorter version, they can be written shorter in php as well.
This link has a research paper comparing erlang and C++ (https://www.researchgate.net/publication/221211369_Comparing_C_and_ERLANG_for_motorola_telecoms_software)
The C++ code us 7.8 times larger for one of the components and 3 times larger for another component. An interesting quote from the paper (which I think is a summary of Ericssons report regarding the AXD301 ATM switch (which is based on 1M SLOC))
Moreover, the systems developed in Erlang and in conventional languages exhibit similar error rates per unit SLOC, and similar SLOC per unit time programmer productivity. Hence, the Erlang system shows at least a fourfold gain in productivity and reduction in errors
Perhaps modern C++/java are not as verbose as they once were and the gain is no longer as big but many years ago a ruby/python/perl project would see similar gains in reduction in SLOC compared to C++.
Thatās fine different strokes for different folks.
Languages like PHP always made me want to scream, yet, I know many people who love it. Thatās totally cool - the world would be a boring place if we all thought the same.
(Initially, PHP I find abhorrant after near a decade working with it a decade ago, so not a great comparison language ^.^; )
Iād find it far more succinct and safe both to do it in OCaml but I think Ruby is older than OCaml (mid 1990ās or something?), so bad example, but in perl that would be like:
package Ball;
sub new {bless({colour => $_[1]}, $_[0])};
sub is_red {shift->colour == "red"}
Used like:
my $ball = new Ball("green");
print("The ball is not red") if(!$ball->is_red());
If I remember right (been yearsā¦ >.>)
Or C++:
struct Ball {
string colour;
Ball(string colour_) : colour(colour_) {}
bool is_red() { return colour == "red"; }
}
Etcā¦ etcā¦
A class example is a pretty bad example overall. Iām guessing from the ācan express more in less codeā that usually means algorithms, not syntax, hence why a class example was a poor example?
Yeah they really canā¦
Modern āstylesā are significantly shorter yes, but even āback in the dayā they could have been shorter as well, just most people had longer ideas in mind because fortran and so forth. Iād argue that even āback thenā C++ could still be about as succinct as Python in any non-trivial code, while still being substantially safer than the 3 listed languages as well.
PHP is a mishmash of decisions, itās very Perlāish in many ways, but probably better, except itās ecosystem, which is worse. ^.^;
I may hate on PHP but itās not because of the language itself, which actually has some really cool design decisions, but mostly in that the naming conventions are inconsistent, the ecosystem knows no concept of security (though it seems Ruby is similar on that as well from what Iāve read), etcā¦ etcā¦ PHP7+ is actually rather blazing fast as well and on benchmarks Iāve seen it absolutely blowing Ruby away in speed, and it easily can be more succinct than ruby (you can do some rather impressive perl-style-golfing in PHP).
Even then, Iām unsure why Ruby would have been picked over, say, Python, and Iāve not seen anything that says that it would have if Rails was not built on it. Iām unsure what it has over Python as of yet, especially as itās slower and has a slightly worse and more verbose syntaxā¦
/me thinks this may should go in another threadā¦
But still, Iām curious how does Ruby do more in given lines of code than, say, C++? Sure I know C++ has #include
ās and other such noise, but they are collapsed by any decent IDE, and even then Ruby was not competing with C++, it was competing with the likes of Perl, which yes Perlās syntax is a bit of a horror aberration but it still seems far more āexpressiveā than Ruby? And Iām still curious what Ruby gains over Python as well?
Unfortunately i think that not too often
Check out how I used Wireshark to help reduce the tedium of testing my Elixir-based serializers and parses for Bitcoinās binary peer-to-peer protocol:
Generating Test Fixtures with Wireshark
FWIW, the tests resulted in an almost immediate positive return on investment. I immediately noticed an issue with one of my serializers that was causing all kinds of mysterious behavior within my node.
Bring your variables back from the dead! https://engineering.tripping.com/bring-your-variables-back-from-the-dead-ea250cf58ba4
Resurrect your variables after you restart an iex session.
I have recently published a short blog post about supervision strategies and DynamicSupervisor
.
I just published a short blog post about me customizing the parameters of my contoller actions in phoenix:
https://medium.com/@lostkobrakai/alternative-parameters-for-phoenix-controller-actions-edd2b8a4362a
Composing Elixir Plugs in a Phoenix application
23 Mar 18Elixir is a functional language, so itās no surprise that one of the main building blocks of the request-response cycle is the humble Plug. A Plug will take connection struct (see Plug.Conn) and return a new struct of the same type. It is this concept that allows you to join multiple plugs together, each with their own transformation on a Conn struct. A plug can be either a function or a module that implements the Plug behaviour.
http://www.jackmarchant.com/articles/composing-elixir-plugs-in-a-phoenix-application
7 ways of doing Fizz Buzz in Elixir and other clickbaity stuff https://minhajuddin.com/2018/07/06/7-ways-of-doing-fizz-buzz-in-elixir-and-other-clickbaity-stuff/
Composing Ecto queries
Hi folks!
I just published a new blog post: A dive into database multi-tenancy in Elixir with Ecto, and I wanted to share with you all. Itās a blog post with some tips and experiences about the migration of an application with a regular database to a multi-tenancy one in Elixir with Ecto.
http://dreamingecho.es/blog/a-dive-into-database-multi-tenancy-in-elixir-with-ecto
How to use private hex dependencies in your docker images while building your releases https://engineering.tripping.com/how-to-use-private-hex-dependencies-in-your-docker-images-42b2d01d8d70
Nice. Itās always good to remind people that thereās many ways of accomplishing something. Iāve got a post sitting in my backlog somewhere about golfing down a riff on FizzBuzz to follow the style of a Clojure implementation. I might have to post that soon.