C++開発者のためのRubyガイドはじめにあなたがC++開発者で、最近Rubyプログラミング言語に関するあらゆる情報が気になっているとしたら、この記事はまさにあなたにうってつけです。ここでは、C++とRubyの重要な違いを概説し、それぞれの言語で実装された小さいながらも完全なサンプルアプリケーションを紹介します。 ただし、Rubyを覚えることがストレスになる可能性があることは、あらかじめご了承ください。この強力かつシンプルな言語に慣れてしまったら、C++に戻ることが辛くなるかもしれません。 高レベル言語の比較と実行サンプルC++は静的に型付けされたコンパイル言語であり、ハイブリッド型オブジェクト指向の性質を備えています。「静的に型付けされた」とは、すべての式と変数の型がコンパイル時にわかっており、プログラムの実行前に重要な正確性チェックを行えることを意味します。「ハイブリッド型オブジェクト指向」とは、この言語がオブジェクトでないプリミティブ型(int型やfloat型など)を定義していることと、オブジェクトの外に関数を記述できることを意味しています。 Rubyというプログラミング言語は、コードをすばやく簡単に書けるようにすることを目指して考案されたものです。C++とは異なり、きわめて動的なインタプリタ型言語で、一連の強力なライブラリが含まれます。一般的にはスクリプト言語と呼ばれていますが、これは純粋なオブジェクト指向の言語であり、汎用アプリケーションに対応するための表現力を十分に備えています。 Rubyでは、変数を宣言する必要がなく、各ステートメントで変数の型を自由に変更できます。したがって、たとえば次のコードでは変数 x = 10 x += 4 x = "My String" x = [1, "My String", Hash.new ] Rubyの重大な弱点は、インタプリタを使用することです。Rubyの実行時のパフォーマンスは、C++のようなコンパイル言語とは比較になりません。したがって、どんなにRubyの機能が気に入ったとしても、実行時の効率性が必要なのであれば、C++を使い続けた方がいいでしょう。 C++とRubyの重要な違いを理解したところで、それぞれの言語で実装された、小さいながらも完全なサンプルアプリケーションを見てみることにしましょう。このサンプルアプリケーションでは、指定ディレクトリ内の一連のファイルにおける各ワードの出現総数を計算し、出現ワード数のまとめを示すXMLファイルを生成して出力します。C++での実装をリスト、Rubyでの実装をリスト2に示します。サンプルコードのダウンロード用ファイルもご利用ください。 リスト1 C++での実装
// for directory manipulation #include <unistd.h> #include <sys/types.h> #include <dirent.h> // streams and strings #include <iostream> #include <fstream> #include <string> // STL collections #include <map> #include <set> struct my_compare { bool operator() (const std::pair<std::string, int> &f1, const std::pair<std::string, int> &f2) { return f1.second < f2.second; } }; struct word_count { std::string word; int total_count; typedef std::multiset< std::pair<std::string, int>, my_compare > file_set_t; word_count(std::string _word) : word(_word), total_count(0) {} virtual void add(std::string filename) { total_count += 1; } virtual file_set_t file_occurrences() { return file_set_t(); } }; bool operator< (const std::pair<std::string, word_count *> &wc1, const std::pair<std::string, word_count *> &wc2) { return wc1.second->total_count < wc2.second->total_count; } bool operator< (const word_count &wc1, const word_count &wc2) { return wc1.total_count < wc2.total_count; } struct word_and_file_count : public word_count { std::map<std::string,int> file_count; word_and_file_count(std::string _word) : word_count(_word) {} void add(std::string filename) { if (file_count.find(filename) == file_count.end()) file_count[filename] = 0; file_count[filename] += 1; total_count += 1; } word_count::file_set_t file_occurrences() { return word_count::file_set_t ( file_count.begin(), file_count.end() ); } }; template <typename W> class word_counter { private: std::map<std::string, word_count *> word_counts; const std::string directory_name; void count_words_in_file(const std::string &filename) { int i = 0; std::ifstream file(filename.c_str()); if ( file.is_open() ) { while (file.good()) { std::string line; getline(file,line); char c = line[i=0]; while ( c != ’ 関連記事 最新トップニュース
|
最安値で落札する逆オークションサイト「ラキオク」(9月8日 17:50)
日本 HP、14.1インチワイド液晶搭載の個人向けノート PC を発売(9月8日 13:40)
働くクルマを運転できるテーマパーク「DIGGER LAND」(9月8日 09:00)
人権活動家を狙った標的型攻撃が出現、F-Secure(9月8日 17:30)
MSN、初の「Wallpaper AD」を採用したキャンペーンを開始(9月8日 14:30)
私の周りは“geek out”している人ばかり(9月5日)
|