sybind
sybind

Reputation: 3588

how to show the only instance methods?

Is there a way to see the class specific methods available to an instance through IRB?

I made an instance of the URI class, and then pressed Tab to see what methods I can use, however I see about 100 possibilities:

  1.9.3p286 :001 > require 'uri'
=> true

1.9.3p286 :002 > uri = URI('http://game.dl.a-steroids.com/TrafficServer/')
=> #<URI::HTTP:0x00000000eae390 URL:http://game.dl.a-steroids.com/TrafficServer/>

  1.9.3p286 :008 > uri.
  Display all 102 possibilities? (y or n)

I want to filter only the specific methods for that instance, such as the ones described here: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html or below:

  1.9.3p286 :003 > uri.host
=> "game.dl.a-steroids.com"

1.9.3p286 :006 > uri.path
=> "/TrafficServer/"

1.9.3p286 :007 > uri.scheme
=> "http"

Upvotes: 2

Views: 257

Answers (3)

sawa
sawa

Reputation: 168269

URI is a module. It cannot have an instance and it does not have instance methods. To see the module methods directly defined on URI, do:

URI.methods(false)

# => [:scheme_list, :split, :parse, :join, :extract, :regexp, :encode_www_form_component, :decode_www_form_component, :encode_www_form, :decode_www_form]

And what URI(...) creates is an instance of URI:HTTP. To see the instance methods directly defined on URI::HTTP, do this:

URI::HTTP.instance_methods(false)

# => [:request_uri]


If this seems too narrow, you can step up to a superclass. URI classes are based on URI::Generic,

URI::HTTP.superclass

# => URI::Generic

so do the same with it:

URI::Generic.instance_methods(false)

#=> [:default_port, :scheme, :host, :port, :registry, :path, :query, :opaque, :fragment, :parser, :component, :set_scheme, :scheme=, :userinfo=, :user=, :password=, :set_userinfo, :set_user, :set_password, :userinfo, :user, :password, :set_host, :host=, :hostname, :hostname=, :set_port, :port=, :set_registry, :registry=, :set_path, :path=, :set_query, :query=, :set_opaque, :opaque=, :set_fragment, :fragment=, :hierarchical?, :absolute?, :absolute, :relative?, :merge!, :merge, :+, :route_from, :-, :route_to, :normalize, :normalize!, :to_s, :==, :hash, :eql?, :component_ary, :select, :inspect, :coerce]

Upvotes: 4

the Tin Man
the Tin Man

Reputation: 160631

I use this in IRB often:

uri.methods - Object.methods

=> [
      :+, :-, :absolute, :absolute?, :coerce, :component, :component_ary,
      :default_port, :fragment, :fragment=, :hierarchical?, :host,
      :host=, :hostname, :hostname=, :merge, :merge!, :normalize,
      :normalize!, :opaque, :opaque=, :parser, :password, :password=,
      :path, :path=, :port, :port=, :query, :query=, :registry,
      :registry=, :relative?, :request_uri, :route_from, :route_to,
      :scheme, :scheme=, :select, :set_fragment, :set_host,
      :set_opaque, :set_password, :set_path, :set_port, :set_query,
      :set_registry, :set_scheme, :set_user, :set_userinfo, :user,
      :user=, :userinfo, :userinfo=
    ]

Upvotes: 1

qqx
qqx

Reputation: 19495

You can add something like the following to your .irbrc file, then all objects will get a #own_methods class that you can use to get that list.

class Object
  def own_methods
    self.class.instance_methods - self.class.superclass.instance_methods
  end
end

Upvotes: 1

Related Questions