人気ブログランキング | 話題のタグを見る

RoR Class のメソッド

Ruby の基本的な使い方は new コンストラクタでクラスからオブジェクトを作ってそれを操作することになる。

オブジェクトにどういう操作をすることができるのかは、そのクラスのメソッドをみれば分かる。例えば A というクラスがあれば、A.methods あるいは、A.instance_methods でメソッドの名前のリストを取得することができる。

また、ユーザ定義の全てのクラスは、Class クラスを継承しているので、Classクラスのメソッドを引き継ぐことになる。したがって、Class クラスのメソッドを調べれば、Rails で使うオブジェクトに共通なメソッドを知ることができる。

このエントリーの最後の図1に、Rails の Class クラスのメソッドを表示させたが、かなり拡張されている。例えば、to_json や to_yaml が使えるようになっているので次の例のように、script/console 上では、オブジェクトを簡単に JSON や YAML の文字列に変換することができる。

~/console/rails/demo$ script/console
Loading development environment (Rails 2.1.1)
>> a = {'apple' => 1, 'orange'=> 2, 'lemon' => 3}
=> {"apple"=>1, "orange"=>2, "lemon"=>3}
>> puts a.to_json
{"apple": 1, "orange": 2, "lemon": 3}
=> nil
>> puts a.to_yaml
---
apple: 1
orange: 2
lemon: 3
=> nil

図1. Rails script/console の Class のメソッド

~$ cd console/rails/demo/
~/console/rails/demo$ ruby script/console
Loading development environment (Rails 2.1.1)
>> puts Class.methods.sort
<
<=
<=>
==
===
=~
>
>=
__id__
__send__
`
acts_like?
alias_attribute
alias_method_chain
allocate
ancestors
as_load_path
attr_accessor_with_default
attr_internal
attr_internal_accessor
attr_internal_naming_format
attr_internal_naming_format=
attr_internal_reader
attr_internal_writer
autoload
autoload?
b64encode
blank?
breakpoint
cattr_accessor
cattr_reader
cattr_writer
class
class_eval
class_inheritable_accessor
class_inheritable_array
class_inheritable_array_writer
class_inheritable_hash
class_inheritable_hash_writer
class_inheritable_reader
class_inheritable_writer
class_variable_defined?
class_variables
clone
const_defined?
const_get
const_missing
const_set
constants
copy_instance_variables_from
daemonize
dclone
debugger
decode64
decode_b
delegate
deprecate
deprecated_method_warning
deprecation_horizon
display
dup
duplicable?
enable_warnings
encode64
enum_for
eql?
equal?
extend
extend_with_included_modules_from
extended_by
find_hidden_method
freeze
frozen?
gem
hash
id
include?
include_all_modules_from
included_in_classes
included_modules
inheritable_attributes
inspect
instance_eval
instance_exec
instance_method
instance_methods
instance_of?
instance_values
instance_variable_defined?
instance_variable_get
instance_variable_names
instance_variable_set
instance_variables
is_a?
kind_of?
load
local_constant_names
local_constants
mattr_accessor
mattr_reader
mattr_writer
method
method_added
method_defined?
methods
model_name
module_eval
name
nesting
new
nil?
object_id
parent
parents
private_class_method
private_instance_methods
private_method_defined?
private_methods
protected_instance_methods
protected_method_defined?
protected_methods
public_class_method
public_instance_methods
public_method_defined?
public_methods
rails_original_const_missing
read_inheritable_attribute
remove_class
remove_subclasses
remove_subclasses_of
require
require_association
require_dependency
require_library_or_gem
require_or_load
reset_inheritable_attributes
respond_to?
returning
send
send!
silence_stderr
silence_stream
silence_warnings
singleton_methods
subclasses
subclasses_of
superclass
superclass_delegating_accessor
superclass_delegating_reader
superclass_delegating_writer
suppress
taguri
taguri=
taint
tainted?
to_a
to_enum
to_json
to_param
to_query
to_s
to_yaml
to_yaml_properties
to_yaml_style
type
unloadable
untaint
with_options
write_inheritable_array
write_inheritable_attribute
write_inheritable_hash
yaml_as
yaml_tag_class_name
yaml_tag_read_class
yaml_tag_subclasses?
by tnomura9 | 2008-11-02 05:50 | Ruby | Comments(0)
<< Google で辞書を使う RoR script/cons... >>