Is it valid to make Ruby Gems an analogy to Java JARs?

Is it valid to make Ruby Gems an analogy to Java JARs?

青萝楚歌 发布于 2021-11-28 字数 412 浏览 774 回复 3 原文

I believe I have put the question quite clearly and in a concise manner. Why do I ask?

I am to explain Ruby on Rails framework to students and this requires me to make some analogies to the Java world (as the course is quite Java-centric). I have no hands-on experience with Ruby on Rails but I sense the Gem/Jar analogy is a valid one.

Could anyone perhaps shed more light on this issue?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝颜夕 2022-06-07 3 楼

As a simple explination comparing them to JARs is good enough for a basic Ruby class. For a more detailed explination see holychao's answer. The analigy that comes to mind for me is that Gem's are to Ruby what CPAN is to Perl.

似最初 2022-06-07 2 楼

I'm not sure is that easy...

I think that a Gem is basically the same thing as a jar, but a Gem has to have a name and a version, a jar does not have this, hm, let's say..., restrictions.

The Jar mechanism was created for packaging and just for packaging.
The most important, IMHO, in the Gem is the RubyGems project
The RubyGems feature set is a little larger than just packaging, in involves package distribution and installation.

I think that the Apache Maven distribution and installation mechanism is the closest thing the Java community has from the Ruby Gems.

无人问我粥可暖 2022-06-07 1 楼

As a short answer I would say: Yes, it is valid.

As a long answer I would say: Yes, it is valid, but there are some important distinctions you may want to describe as well.

A jar has some qualities that makes it very different from a gem. JARs are packaged executable libraries that you generally have to explicitly declare a dependency upon in a Java program's execution at invocation time (by declaring the jar as a dependency when you invoke the java interpreter). The jar has little structure enforced upon it other than a few well defined locations for files (e.g. directory structure must mirror package path).

A Gem is a descriptor for installing a library to a system permanently as well as a package of functionality made available to be declared a dependency during runtime. A gem has a strict versioning syntax as part of its definition, and gems tend to be distributed from few centralized repositories that spend a decent amount of effort to ensure uniqueness in gem names. Gems can explicitly declare their dependencies on other gems. (Contrast with JARs where you must make sure you have all of the dependencies for a jar satisfied at invocation time and jars do not explicitly adopt the responsibility for dependency declaration OR resolution). Additionally there is some optional functionality built into the gem tool that can be quite convenient, for example you can declare a default executable for a gem to be invoked if a user wishes to "run a gem", and you can declare a unit testing suite for a gem so that a user who installs a gem can ensure its functionality.

Also you might want to describe to your students that RubyGems is a tool created independently of the Ruby language itself, and that it benefits after many of the inconveniences and complaints that arose from managing jars had been well exposed from years of Java development.