Python is a unique language in that it is fairly easy to learn, given its straight-forward syntax, yet still extremely powerful. There are a lot more features under the hood than you might realize. While I could be referring to quite a few different things with this statement, in this case I'm talking about the decorators @classmethod and @staticmethod. For many of your projects, you probably didn
皆さんこんにちは!「太鼓式マッサージ?面白そう!」と思ったら「タイ古式マッサージ」でした。MUGENUP の osada です。名前って難しいですよね。 ところで、ActiveSupport::Concern の ClassMethod で定義したクラス変数が、どこにあるか、ご存知ですか? # app/models/concern/item_module.rb module ItemModule extend ActiveSupport::Concern module ClassMethods def my_module_method @@concern_class_variable ||= "concern_class_variable" end end end # app/models/item.rb class Item < ActiveRecord::Base include It
Rails のコードを読んでいると、module ClassMethods って多く書かれている事が分かると思いますが、こいつが何をやっているのかを話していきます。 module ClassMethods を説明する前に、まず、以下のような Module と Class を定義します。 module ModuleA def self.class_method_a 'class_method_a' end def instance_method_a 'instance_method_a' end end class ClassA include ModuleA end
AWS SDK for Ruby 今年の7月にAWS SDK for Rubyがリリースされました。いつもはフルセットで用意されているJavaを使っているのですが、今回はRubyがどの程度簡単に記述できるのか確認するためにサンプルを作りました。また、サンプル作成にあたって、SNSとSQSの連携を試しました。 Amazon LinuxでAWS SDK for Rubyのセットアップ 早速セットアップしましょう。 $ sudo yum install ruby-devel rubygems gcc make libxml2-devel libxslt-devel aws-sdk Amazon SNSをセットアップする 通知先としてAmazon SNSのトピックを作成します。トピックは、受付窓口として対応します。受け付けられたメッセージはサブスクライバ(購読者)に渡されます。サブスクライバには、
Class methods are the source for continuous discussions and disagreements among my colleagues. While some consider them precise and helpful, others feel they are actually pesky and that they tend to make code harder to read and manage. As for me, I find that the truth tends to lean to the latter; I embrace Ruby’s OO nature and I like to think (and read!) in objects. Having said that, sometimes cla
read(length = nil, str = nil) -> String[permalink][rdoc] ARGVに指定されたファイルを先頭のファイルからlengthバイト読み込み、 その文字列をstrに出力します。読み込んだ文字列を返します。 [PARAM] length: 読み込むバイト数を指定します nilの場合はARGVのすべてのファイルを読み込みます。 [PARAM] str: 出力先の文字列 # カレントディレクトリから適当にファイルを選ぶ ARGV.replace( Dir.glob("*").select{|name| FileTest.file?(name)} ) str = "efefefe" ARGF.read(100, str) p str.size #=> 100 p str # 例 #=> "cat:\n - name: taro\n age: 7\n
Classes in Ruby are first-class objects—each is an instance of class Class. -- ruby-doc.orgMost classes are defined using the syntax of the following example. class Person ... end Defining classes this way is familiar to most developers. Ruby provides another syntax for defining classes using Class.new. The following example also defines the Person class. Person = Class.new do ... endClass.new(sup
タイトル メタプログラミングRuby 著者 Paolo Perrotta (著), 角征典 (翻訳) 出版社 アスキー・メディアワークス Amazonで購入する Ruby が人気のあるプログラミング言語である理由のひとつに、強力なメタプログラミングがあると思います。Ruby の本当の力を知るのは、このメタプログラミングを理解したときだとも思います。 本書は、Ruby の内部動作をきちんと説明した上で、メタプログラミングの説明をしてくれます。とても丁寧な説明で、少しでもプログラミングの知識があれば理解できる文章になっています。 プログラミング能力を高めるひとつの方法に、良いソースコードを読むことというのがあります。Ruby で書かれたフレームワークやライブラリのソースコードを読むと、なんでこんな動作になるんだろう?という疑問や、どうしてこんな風に書けるのだろう?という疑問が湧いてきます。 本
Ruby プログラムの実行は文の連なりの評価です。なんらかの形であたえられたプログラムテキストをコンパイルし、BEGIN 文があればそれを評価し、トップレベルの式の連なりを評価し、END ブロックがあれば最後にそれを評価して終了します (終了処理の詳細については 終了処理 を参照のこと)。 文 if if 文は、まず条件式を評価し、その値が真ならば対応する本体を評価します。偽ならば elsif 節の条件式を順番に評価し、その値が始めて真になった節の本体を評価します。それらがすべて偽なら else 節の本体を評価します。 文全体の値は最後に実行した本体の値です。ただし評価された本体に式がなかった場合、あるいはすべての条件式が偽でかつ else 節もなかった場合は nil です。 until if 修飾子 unless 修飾子 while until while 修飾子 until 修飾子 f
A couple days ago I pointed out that learning ruby and rails wasn't trivial because of how different things are. Luckily I knew this before starting so roadblocks, while disappointing, aren't crushing. Today though I have some slightly better news as I feel that I've taken a small, but important step. I don't really understand how it happens, but everything in ruby is an object. You might think th
インスタンス変数 (instance variable) とは、Smalltalk等のオブジェクト指向言語において、オブジェクトのインスタンスごとに割り当てられた変数のことである。フィールド (field)、データメンバ (data member)、メンバ変数 (member variable) とも呼ばれる。それに対してクラス変数は、同じクラスに対する全てのインスタンスが同じ値を共有する。 インスタンス変数は、インスタンスフィールド (instance field) あるいは、略して「フィールド」と呼ぶこともある (本来、この「フィールド」の意味には、「クラス変数」も含まれるが、プログラミング言語によってはインスタンス変数のことを「フィールド」と呼び、クラス変数とは名前を区別しているもの[要説明]もある)。UMLではインスタンス変数のことを属性 (attribute) とも呼ぶ。 Jav
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く