日本語の解説が後半にあります。

What is this program?

The program ‘calc’ is a scientific calculator written in Ruby programming language. It is run from the command line like this.

$ calc 2*3+4*5
26
$ calc
calc > 0.1-0.2
-0.1
calc > sin(PI/6)
0.49999999999999994 # Float always includes error.
calc > quit
$

Prerequisites

installation

There are two ways.

  1. Click the ‘Code’ button, then click ‘Download ZIP’ in the small dialog.

  2. Unzip the downloaded Zip file, then a new directory ‘calc’ will be created.

  3. Type gem build s_calc under the directory ‘calc’, then the gem file s_calc-X.X.X.gem is created. The part X.X.X is a version number like 0.1.4.

  4. Type gem install s_calc-X.X.X.gem.

Another way is simpler. This gem is published to RubyGems.org. You can install it from RubyGems. Just type:

$ gem install s_calc

Be careful. The command name and GitHub repository name is calc but the gem name is s_calc. This is because the name calc has already existed in RubyGems.org.

How to use the program.

  1. Start the terminal and type calc.

  2. Then the prompt appears calc >

  3. Type an expression like 2+3 and push Enter key, then the answer 5 appears.

  4. Type quit, q or exit, then the program will finish.

  5. You can also use it as a one line calculator like $ calc "10*20+30".

feature

Functions

Racc library

Racc is a Ruby standard library. It is a parser generator like Yacc, which is a famous parser generator made for the Unix Operating system. Calc uses Racc. It makes the library file lib/calc/calc.rb with Racc. The source file is racc/calc.y. You can compile it by typing:

$ rake

See racc/doc.md for details.

Rakefile

You can do the following

Using Calc as a library

You can use Calc as a library in your Ruby program.

The following is a sample code.

require 'calc'

c = Calc.new
s = "1+2"
print "#{c.run(s)}\n" #=> 3.0

License

GPL ver 3 or later. See License.md for details.

これは何のプログラム?

Rubyプログラムcalc.rbは関数電卓です。 コマンドラインから次のように起動できます。

$ calc 2*3+4*5
26
$ calc
calc > 0.1-0.2
-0.1
calc > sin(PI/6)
0.49999999999999994 # 実数計算では誤差が発生します
calc > quit
$

動作条件

インストール

2通りのインストール方法があります。

  1. ‘Code’ボタンをクリックし、現れた小さなダイアログの‘Download ZIP’をクリックする

  2. ダウンロードしたZipファイルを解凍する。これにより新しく「calc」ディレクトリが作られ、その中にすべてのファイルが収められる

  3. 「gem build s_calc」を実行することにより、gemファイル「s_calc-X.X.X.gem」が作られる。「X.X.X」の部分はバージョン番号で、例えば「0.1.4」のような数字である

  4. 「gem install s_calc-X.X.X.gem」とタイプし、gemをインストールする

もっと簡単なインストール方法は、RubyGemsからインストールすることです。 次のようにタイプするだけでgemをダウンロードしインストールします。

$ gem install s_calc

コマンド名とGitHubのレポジトリ名がcalcであるのに対して、gem名はs_calcであることに注意してください。 これは、RubyGemsには既にcalcという名前のgemが存在しており、異なるgem名が必要だったためです。

使い方

  1. 解凍したフォルダで端末(コマンドライン)を起動し、「calc」と入力する

  2. プロンプト「calc >」が現れる

  3. 式(例えば2+3)と入力し、エンターキーを押すと、答え「5」が表示される

  4. 「quit」または「q」または「exit」とタイプするとプログラムは終了する

  5. 引数を付けて一行計算アプリとしても使える。(例)$ calc "10*20+30"

特長

関数

Racc

RaccはRubyの標準ライブラリーです。 パーサー・ジェネレーターと呼ばれ、Unixオペレーティングシステム上の有名なYaccに似ています。 CalcはRaccを使ってlib/calc/calc.rbを生成しています。 そのソース・ファイルはracc/calc.yです。 コンパイルは次のようにタイプするだけでできます。

$ rake

ドキュメント racc/doc.mdを参照してください。

Rakefile

次のことができます。

Calcをライブラリとして使う

CalcをライブラリとしてRubyプログラムの中で使うことができます。

以下に簡単なサンプルコードを示します。

require 'calc'

c = Calc.new
s = "1+2"
print "#{c.run(s)}\n" #=> 3.0

ライセンス

Copyright © 2022,2023 ToshioCP (関谷 敏雄)

このプログラムは、フリーソフトウェア財団によって発行された「GNU一般公衆利用許諾書」(バージョン3か、希望によってはそれ以降のバージョンのうちどれか)の定める条件の下で再頒布または改変することができる。

このプログラムは有用であることを願って頒布されますが、全くの無保証 です。商業可能性の保証や特定の目的への適合性は、言外に示されたものも含め全く存在しません。 詳しくはGNU 一般公衆利用許諾書(英語)、またはその日本語訳GNU 一般公衆利用許諾書の日本語訳をご覧ください。

なお、ライセンスを英語で記したLicense.mdもあります。