As you may know Ruby 2.0.0 has been released. Despite the major version it is mostly an incremental release. However, it does include a few breaking changes so a major version is warranted. However, whilst the usefulness of most of the new features was obvious to me, I couldn’t say the same of Module#prepend. However, whilst listening to the Ruby Rogues podcast on Ruby 2 one of the Rogues (I think
You've seen SimpleDelegator in action and used it a bit yourself. The delegate library is more than just a fancy method_missing wrapper. Easy Wrappers First and foremost, SimpleDelegator is a fancy method_missing wrapper. I know I said the library was more than that, just bear with me. Here's some sample code: jim = Person.new # some object class Displayer < SimpleDelegator def name_with_location
As promised, we’re back with more design discussion and hopefully some interesting ideas. Let’s start with a quick recap of what has happened so far. First, Sandi Metz posted on her blog about type specific coupling that a case statement can introduce. Her proposed solution encouraged us to pass the responsibilities down to the individual objects, which is certainly desirable. Aaron Patterson got
send(name, *args) -> object[permalink][rdoc][edit] send(name, *args) { .... } -> object __send__(name, *args) -> object __send__(name, *args) { .... } -> object オブジェクトのメソッド name を args を引数にして呼び出し、メソッドの実行結果を返します。 ブロック付きで呼ばれたときはブロックもそのまま引き渡します。 send が再定義された場合に備えて別名 __send__ も用意されており、ライブラリではこちらを使うべきです。また __send__ は再定義すべきではありません。 send, __send__ は、メソッドの呼び出し制限にかかわらず任意のメソッドを呼び出せます。 クラス/メソッドの定義/呼び出し制限 も参照
『るびま』は、Ruby に関する技術記事はもちろんのこと、Rubyist へのインタビューやエッセイ、その他をお届けするウェブ雑誌です。 Rubyist Magazine について 『Rubyist Magazine』、略して『るびま』は、Rubyist の Rubyist による、Rubyist とそうでない人のためのウェブ雑誌です。 最新号 Rubyist Magazine 0064 号 バックナンバー Rubyist Magazine 0064 号 Rubyist Magazine 0063 号 Rubyist Magazine 0062 号 Kaigi on Rails 特集号 RubyKaigi Takeout 2020 特集号 Rubyist Magazine 0061 号 Rubyist Magazine 0060 号 RubyKaigi 2019 直前特集号 Rubyist
We couldn’t find the page you are looking for. Perhaps you can try searching:
標準クラス・モジュール > Object > method_missing def method_missing(method_name [, *args [, &block]]) code... end method_missingメソッドを定義すると、存在しないメソッドが呼ばれたときの動作を記述できます。メソッド名の文字列を使って動作を変えるような機能が作れます。 オブジェクトに対して存在しないメソッドを呼び出すと、Rubyはmethod_missingメソッドを呼び出します。引数method_nameには、メソッド名がシンボルで渡されます。引数*argsと&blockでメソッドに渡された引数とブロックを取り出せます。戻り値は存在しないメソッドの戻り値になります。 次の例では、Fixnumクラスにmethod_missingを定義して、「to_数字」というメソッドが呼ばれたときは、整数
10 things you should know about method_missing Posted by amy on August 01, 2007 [update, 9:27 a.m., 8/1/2007: welcome, reddit readers. annoyed at me for calling this post “10 things”? Well, then you’ll really hate me after reading this post!] 1. method_missing is a Ruby kernel method and everyone should know about it. 2. Rails implements some of its funkiest magic with method_missing. When you
2010年01月27日09:23 Ruby Rubyのmethod_missingを使って黒魔術を実装する Rubyにはメソッド探索の最後に呼ばれる(つまり、メソッドが見つからないときに呼ばれる)フック的なメソッドとして method_missing というメソッドがあります。これを上手に利用することで黒魔術的なコードが書けてかっこいいですw #!/usr/bin/ruby module Sasata299 # 適当なモジュールを定義 def hoge(num) return num * 2 end def fuga(num) return num + '299' end end def method_missing(action, *args) if action.to_s =~ /(.+)_(.+)/ eval "include #{$1.capitalize}" __send__ $
クラスの特異クラスにメソッドを追加すればクラスメソッドを追加できる。 class Hoge class << self ["method_a", "method_b"].each do |method_name| define_method method_name do |param| "#{param} was passed to #{method_name}" end end end end クラス定義外で以下のようにしても同じ。 class << Hoge ["method_a", "method_b"].each do |method_name| define_method method_name do |param| "#{param} was passed to #{method_name}" end end end 理屈としては クラスメソッドはクラスの特異メソッドである 特
remove_method(*name) -> self[permalink][rdoc][edit] インスタンスメソッド name をモジュールから削除します。 Ruby 1.8.0 以降は複数のメソッド名を指定して一度に削除できます。 [PARAM] name: 0 個以上の String か Symbol を指定します。 [EXCEPTION] NameError: 指定したメソッドが定義されていない場合に発生します。 例 class C def foo end remove_method :foo remove_method :no_such_method # 例外 NameError が発生 end [SEE_ALSO] Module#undef_method
TL;DR: depending on your app, using define_method is faster on boot, consumes less memory, and probably doesn’t significantly impact performance. Throughout the Rails code base, I typically see dynamic methods defined using class_eval. What I mean by “dynamic methods” is methods with names or bodies that are calculated at runtime, then defined. For example, something like this: class Foo class_eva
はじめに Rubyでは、通常のメソッド定義のほかに、動的にメソッドを定義する方法がいくつかありますが、 動的に定義されたメソッドと通常のメソッドの呼出コストの差が気になります。 また、動的に定義されたメソッドの中でもdefine_methodで定義されたものは重いということもよく知られています。 一例として、Railsでは、重いdefine_methodを使う代わりに文字列のclass_evalを使用している箇所が多くあります。 というわけで、 通常のメソッド定義 define_method class_eval + 文字列 class_eval + ブロック で定義したメソッドの呼出コストのベンチマークを取ってみました。 class_eval + ブロックは 正確には動的ではない(メソッドの内容を動的に変更できない)のですが、気になったので同時にベンチマークすることにしました。 ベンチマ
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く