Created
July 31, 2011 04:37
-
-
Save yaotti/1116402 to your computer and use it in GitHub Desktop.
#ruby でobject.fieldを得るときに,objectがnilのときはnilを返して,そうでないときはobject.fieldを返すようなメソッドってあるのかな.NilClass.instance_methodsを見たけどそれらしいものはなかった
This file contains hidden or 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 User | |
attr_accessor :name | |
def initialize name=nil | |
self.name = name | |
end | |
end | |
me = User.new 'me' | |
p me.name.length # => 2 | |
unknown = User.new | |
p unknown.name.length # => error! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment