Last active
October 7, 2015 10:58
-
-
Save higaki/3154679 to your computer and use it in GitHub Desktop.
learn ruby #43 @Ruby-KANSAI #55
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# | |
# | |
class Person; end | |
obj = Person.new | |
# => #<Person:0x0000010104a108> | |
obj.class # => Person | |
Person.ancestors | |
# => [Person, Object, PP::ObjectMixin, Kernel, BasicObject] | |
Person.superclass # => Object | |
# | |
# | |
# | |
class Person | |
def initialize name | |
@name = name | |
end | |
end | |
matz = Person.new('matz') | |
# => #<Person:0x000001010463a0 @name="matz"> | |
# | |
# | |
# | |
class Person | |
attr_reader :name | |
end | |
matz.name # => "matz" | |
# | |
# | |
# | |
class Person | |
def initialize name, born = nil | |
@name, @born = name, born | |
end | |
attr_accessor :born | |
end | |
matz.methods.map(&:to_s).grep(/born/) | |
# => ["born", "born="] | |
# | |
# | |
matz.born = Time.local(1965, 4, 14) | |
dhh = Person.new('dhh', | |
Time.local(1979, 10, 15)) | |
matz.born | |
# => 1965-04-14 00:00:00 +0900 | |
dhh.born | |
# => 1979-10-15 00:00:00 +0900 | |
# | |
# | |
# | |
class Person | |
def age | |
(Time.now.strftime('%Y%m%d').to_i - | |
@born.strftime('%Y%m%d').to_i) / | |
10000 | |
end | |
end | |
matz.age # => 47 | |
dhh.age # => 32 | |
# | |
# | |
# | |
matz.to_s # => "#<Person:0x000001010463a0>" | |
matz.method(:to_s) # => #<Method: Person(Kernel)#to_s> | |
Person.ancestors # => [Person, Object, Kernel, BasicObject] | |
class Person | |
def to_s | |
"#{@name}(#{age})" | |
end | |
end | |
matz.to_s # => "matz(47)" | |
dhh.to_s # => "dhh(32)" | |
# | |
# | |
# | |
person = Marshal.load(Marshal.dump matz) | |
person == matz # => false | |
person == dhh # => false | |
# | |
# | |
# | |
class Person | |
include Comparable | |
def <=> o | |
@name <=> o.name | |
end | |
end | |
person == matz # => true | |
person == dhh # => false | |
matz > dhh # => true | |
# | |
# | |
# | |
people = [matz, dhh] | |
people.sort # => [dhh(32), matz(47)] | |
# | |
# | |
# | |
h = {matz => "Ruby", dhh => "Rails"} | |
h[matz] # => "Ruby" | |
h[dhh] # => "Rails" | |
key = Marshal.load(Marshal.dump matz) | |
key == matz # => true | |
h[key] # => nil | |
# | |
# | |
# | |
class Person | |
def hash | |
code = 17 | |
code = 37 * code + @name.hash | |
code = 37 * code + @born.hash | |
end | |
end | |
matz.hash # => 25157996556348736046 | |
dhh.hash # => 78705111758323992282 | |
# | |
# | |
# | |
class Person | |
def eql? o | |
return false unless @name.eql? o.name | |
return false unless @born.eql? o.born | |
true | |
end | |
end | |
key.eql? matz # => true | |
key.eql? dhh # => false | |
# | |
# | |
# | |
h = {matz => "Ruby", dhh => "Rails"} | |
h[matz] # => "Ruby" | |
h[dhh] # => "Rails" | |
h[key] # => "Ruby" | |
# | |
# | |
# | |
class Person | |
protected :born | |
end | |
# matz.born | |
# # ~> -:172:in `<main>': protected method `born' called for matz(47):Person (NoMethodError) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment