{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### 10-1-2 オブジェクトが持つメソッドを調べる\n", "#### オブジェクトの持つメソッドを一覧で確認する" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":singleton_method" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "class ParentClass\n", " def super_public_method; end\n", " \n", " private\n", " def super_private_methid; end\n", " \n", " protected\n", " def super_protected_method; end\n", "end\n", "\n", "class ChildClass < ParentClass\n", " def public_method; end\n", " \n", " private\n", " def private_method; end\n", " \n", " protected\n", " def protected_method; end\n", "end\n", "\n", "child = ChildClass.new\n", "\n", "def child.singleton_method; end" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[:singleton_method, :public_method, :protected_method, :super_public_method, :super_protected_method, :pry, :__binding__, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :to_json, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :define_singleton_method, :method, :remove_instance_variable, :instance_variable_set, :extend, :to_enum, :enum_for, :pretty_inspect, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :display, :to_s, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]\n", "[:singleton_method, :public_method, :super_public_method, :pry, :__binding__, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :to_json, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :define_singleton_method, :method, :remove_instance_variable, :instance_variable_set, :extend, :to_enum, :enum_for, :pretty_inspect, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :display, :to_s, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]\n", "[:private_method, :super_private_methid, :timeout, :DelegateClass, :Digest, :sprintf, :format, :Integer, :Float, :String, :Array, :Hash, :fail, :iterator?, :__method__, :catch, :__dir__, :loop, :global_variables, :throw, :block_given?, :raise, :__callee__, :eval, :Pathname, :Rational, :jj, :trace_var, :untrace_var, :set_trace_func, :at_exit, :Complex, :gem, :select, :caller, :caller_locations, :`, :test, :fork, :exit, :gem_original_require, :JSON, :sleep, :respond_to_missing?, :pp, :j, :load, :exec, :system, :exit!, :abort, :spawn, :syscall, :open, :printf, :print, :putc, :puts, :gets, :readlines, :readline, :initialize_copy, :initialize_clone, :initialize_dup, :srand, :p, :rand, :lambda, :trap, :proc, :require, :require_relative, :autoload, :autoload?, :binding, :URI, :local_variables, :warn, :method_missing, :singleton_method_added, :singleton_method_removed, :singleton_method_undefined, :initialize]\n", "[:protected_method, :super_protected_method]\n" ] }, { "data": { "text/plain": [ "[:singleton_method]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p child.methods\n", "p child.public_methods\n", "p child.private_methods\n", "p child.protected_methods\n", "child.singleton_methods" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[:singleton_method]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "child.methods(false)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[:singleton_method, :singleton_method_from_add_singleton_method]\n", "[:singleton_method]\n" ] }, { "data": { "text/plain": [ "[:singleton_method]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "module AddSingletonMethodModule\n", " def singleton_method_from_add_singleton_method; end\n", "end\n", "\n", "child.extend AddSingletonMethodModule\n", "\n", "p child.singleton_methods # == child.singleton_methods(true)\n", "p child.singleton_methods(false)\n", "child.methods(false)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[:singleton_method, :singleton_method_from_add_singleton_method, :public_method]\n", "[:private_method]\n" ] }, { "data": { "text/plain": [ "[:protected_method]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p child.public_methods(false)\n", "p child.private_methods(false)\n", "child.protected_methods(false)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[:singleton_method, :singleton_method_from_add_singleton_method, :public_method, :super_public_method, :pry, :__binding__, :pretty_print, :pretty_print_cycle, :pretty_print_instance_variables, :pretty_print_inspect, :to_json, :instance_of?, :kind_of?, :is_a?, :tap, :public_send, :define_singleton_method, :method, :remove_instance_variable, :instance_variable_set, :extend, :to_enum, :enum_for, :pretty_inspect, :<=>, :===, :=~, :!~, :eql?, :respond_to?, :freeze, :inspect, :object_id, :send, :display, :to_s, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variable_get, :instance_variables, :instance_variable_defined?, :!, :==, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "child.public_methods" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[:public_method]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "child.class.public_instance_methods(false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "次回 10-1-2の ■オブジェクトにメソッドが定義されているかを確認する から再開" ] } ], "metadata": { "kernelspec": { "display_name": "Ruby 2.4.1", "language": "ruby", "name": "ruby" }, "language_info": { "file_extension": ".rb", "mimetype": "application/x-ruby", "name": "ruby", "version": "2.4.1" } }, "nbformat": 4, "nbformat_minor": 2 }