I am willing to call a method when trying to display an object, but I don't find which method is used, for example:
[41] pry(main)> u
=> {"id"=>3}
[42] pry(main)> u.inspect
=> "#<User id=3>"
[43] pry(main)> u.to_s
=> "#<User id=3>"
[44] pry(main)> puts c
#<User id=3>
=> nil
I would like to know which method is called for the first case.
User is not an ActiveRecord class, it inherits from Hashie.
Thanks for your help!
It's probably the rails method attributes - though it is a feature of Pry, not of the standard rails console which would give you ruby-1.9.2-p290 :047 > u
=> #<u:0x8a2f6cc #id=3>
Related
I'm running into an issue when trying to create an open struct with an attribute that has the same name as one of the OpenStruct instance methods. Specifically, i'd like to create an open struct that has an attribute capture. I'm using this as a stub in an rspec test, so i can't change the name of the method (it must be capture)
#=> OpenStruct.new(capture: true).capture
#=> ArgumentError: wrong number of arguments (0 for 1)
looking at the OpenStruct methods, it has a method capture and it is this method that is getting called. Is there a way to instantiate an open struct with an attribute of the same name as one of its methods?
for clarity, i specifically need the method capture, which i've confirmed breaks on rails 4.0.x but not rails 5, but this situation holds true for any method openstruct might have.
#=> OpenStruct.new(class: true).class
#=> OpenStruct
This works just fine for me in pry (running ruby 2.3, by the way)
[9] pry(main)> OpenStruct.new(capture: 1).capture
=> 1
Here's another way to do it:
[15] pry(main)> a = OpenStruct.new capture: 1
=> #<OpenStruct capture=1>
[22] pry(main)> a.singleton_class.class_exec { def capture; self[:capture] + 1; end }
=> :capture
[23] pry(main)> a.capture
=> 2
i don't know what testing library you're using, but if it's RSpec, you could use this mocking approach as well:
a = OpenStruct.new capture: 0
allow(a).to receive(:capture).and_return(a[:capture])
a.capture # => 0
I'm trying to understand a mixin precedence situation. If I run foo.bar I'm trying to figure out exactly which bar will be executed. Here is a snipped irb session where I tried to find out where an array's assoc method is defined.
2.3.0 :032 > ray = ['cat', nil, 'dog']
=> ["cat", nil, "dog"]
2.3.0 :033 > ray.methods
=> [:fill, :assoc, :rassoc, :uniq, ...
2.3.0 :034 > ray.method("assoc").class_or_module_name
NoMethodError: undefined method `class_or_module_name' for #<Method: Array#assoc>
from (irb):34
from /Users/mark/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :035 > ray.methods("assoc").class_or_module_name
NoMethodError: undefined method `class_or_module_name' for #<Array:0x007ffb8aa286a8>
from (irb):35
from /Users/mark/.rvm/rubies/ruby-2.3.0/bin/irb:11:in `<main>'
2.3.0 :036 > ray.methods("assoc")
=> [:fill, :assoc, :rassoc, :uniq, :uniq!, :compact, ...
If possible I would like to see the methods and their source location that lost precedence.
To find out which class or module defines a method (rather than what file), use Method#owner.
For example,
[].method(:last).owner #=> Array
[].method(:flat_map).owner #=> Enumerable
You'll actually find that a lot of the methods that Array could get from Enumerable, it actually defines itself (presumably to provide a more efficient implementation):
irb(main):018:0> Enumerable.instance_methods.select {|meth_name| [].method(meth_name).owner == Array }
=> [:to_a, :to_h, :sort, :count, :find_index, :select, :reject, :collect, :map, :first, :include?, :reverse_each, :zip, :take, :take_while, :drop, :drop_while, :cycle]
In general, to know where a method is defined you can use source_location(docs), e.g.
ray.method("assoc").source_location
although in this case it will return nil since assoc is a native method. It will work for other methods, e.g.
ray.methods.map{ |m| [m, ray.method(m).source_location] }
For native methods you can use Pry, e.g.
pry(main)> ? ray.assoc
From: array.c (C Method):
Owner: Array
Visibility: public
[...]
? is a shorthand for show-doc.
I feel this is fundamental to my understanding of Ruby and object-oriented programming in general, so I'm asking this fairly simplistic question here at the risk of looking foolish. I've been toying around with irb. I've created my first ever class:
$ irb
ruby-1.9.2-p290 :001 > class Person
ruby-1.9.2-p290 :002?> attr_accessor :firstname, :lastname, :gender
ruby-1.9.2-p290 :003?> end
=> nil
ruby-1.9.2-p290 :004 > person_instance = Person.new
=> #<Person:0x007f9b7a9a0f70>
ruby-1.9.2-p290 :005 > person_instance.firstname = "Bob"
=> "Bob"
ruby-1.9.2-p290 :006 > person_instance.lastname = "Dylan"
=> "Dylan"
ruby-1.9.2-p290 :007 > person_instance.gender = "male"
=> "male"
So Person.new is my object, right? Or is my object the combination of class Person and the attributes I've defined for that class?
Your object is the result of running Person.new, which you've captured in person_instance.
In ruby, attributes don't actually exist until they are first written, so before person_instance.firstname = "Bob", your instance has no attributes. After executing this statement it has a #firstname attribute, but no others.
You are right. Everything in ruby is an object. So when you create a new class 'person' it itself is an object of type class.
Strings are also objects, so after you've done
person_instance.firstname = "Bob"
then person_instance.firstname refers to a string object. So you can call
# Returns String, indicating that the object returned by
# person_instance.firstname is an instance of the String class.
person_instance.firstname.class
# Returns a not very informative number, but indicates that it is its own object
person_instance.firstname.object_id
I want to understand how Ruby method methods() work.
I tried to Google with "ruby methods" but it's not what i need.
Also i've seen ruby-doc.org, but i didn't find this method.
Can you expain me how it works in details or give me a link?
Update
I experimented with methods() method and got such results:
'lab rat' code
class First
def first_instance_mymethod
end
def self.first_class_mymethod
end
end
class Second < First
def second_instance_mymethod
end
def self.second_class_mymethod
end
end
Work with Classes
#returns available methods list for class and ancestors
puts Second.methods.grep(/mymethod/)
# => second_class_mymethod
# => first_class_mymethod
#returns Class methods list for current class only
puts Second.methods(false)
# => second_class_mymethod
Work with Objects
obj = Second.new
def obj.obj_singleton_mymethod
end
#returns available methods list for object and ancestors
puts obj.methods.grep(/mymethod/)
# => second_instance_mymethod
# => first_instance_mymethod
#returns current object class methods
puts obj.methods(false)
# => obj_singleton_mymethod
The accepted answer misses a slight point. A fuller answer was given in the comment by keymone - .methods returns an array of symbols being names of all the methods defined on the given instance. For example:
irb(main):012:0> object = ""
=> ""
irb(main):013:0> object.instance_eval("def foo;:bar;end")
=> nil
irb(main):014:0> object.methods.include?(:foo)
=> true
irb(main):016:0> "".methods.include?(:foo)
=> false
I'm not entirely sure why it's not in the ruby 1.9 docs (it seems to still be in the code), but you can see the documentation in the 1.8.7 docs: http://www.ruby-doc.org/core-1.8.7/classes/Object.html#M000032
Basically, in ruby 1.9 it just returns a list of the symbols (names) for all the methods in a given class and its ancestors. (ruby 1.8 it returned a list of strings)
I'm on Ruby 2.2.5 and I am unfortunately not getting this to work when passed false any longer. I do remember back on older versions of Ruby, passing false to this method used to return only the class-level methods.
However, as of Ruby 2.2.5, here's what I'm getting
class Thingy < ApplicationRecord
def hello
end
end
class Apple < Thingy
def self.goodbye
end
def booty
end
end
results:
2.2.5 :001 > a = Thingy.new
=> #<Thingy id: nil, created_at: nil, updated_at: nil>
2.2.5 :002 > a.methods(false)
=> []
2.2.5 :003 > b = Apple.new
=> #<Apple id: nil, created_at: nil, updated_at: nil>
2.2.5 :004 > b.methods(false)
=> []
2.2.5 :005 > Apple.methods(false)
=> [:attribute_type_decorations, :_validators, :defined_enums, :goodbye]
2.2.5 :006 > Thingy.methods(false)
=> [:attribute_type_decorations, :_validators, :defined_enums]
2.2.5 :007 >
the only time passing false returns anything interesting to me is to examine class-level methods only (those that start with self.) Otherwise it seems to not be helpful anymore.
irb(main):001:0> require 'active_support'
=> true
irb(main):002:0> require 'active_support/inflector/inflections'
=> true
irb(main):003:0> ActiveSupport::Inflector.pluralize('test')
=> "test"
irb(main):004:0> ActiveSupport::Inflector.singularize('tests')
=> "tests"
irb(main):005:0> ActiveSupport::Inflector.titleize('hat simulator')
=> "Hat Simulator"
<ort::Inflector.tableize("america's number one hat simulator")
=> "america's number one hat simulator"
Well, basically, that's the question. It's confusing me that methods such as titleize seem to work fine, but tableize, pluralize and singularize don't.
Have I forgotten to require something?
(On a separate note, I notice this page provides examples like "post".pluralize, which when I tried, resulted in NoMethodError: undefined method 'pluralize' for "post":String. But maybe that's something to save for another question.)
Access to #pluralize without adding new methods to the String class:
require 'active_support/inflector'
ActiveSupport::Inflector.pluralize('test')
#=> "tests"
For String class:
require 'active_support/core_ext/string'
"test".pluralize
#=> "tests"
which actually calls ActiveSupport::Inflector.pluralize underneath:
def pluralize
ActiveSupport::Inflector.pluralize(self)
end