落書きノート

ふと自分が気になった事を書いてます

Ruby資格問題集

資格問題集、こんな感じで進みました。ところどころ実行し直ししたりして継ぎ接ぎだらけです。今、28%まで進み、残り42%あります…。大丈夫だろうか…。これやり終えたとしても問題が待ってますね。その前に違う問題集で練習するつもりですが。

irb(main):001:0> class Foo
irb(main):002:1> def initialize(a)
irb(main):003:2> @a = a
irb(main):004:2> end
irb(main):005:1> def method1
irb(main):006:2> @a
irb(main):007:2> end
irb(main):008:1> end
=> :method1
irb(main):009:0> foo1 = Foo.new(1)
=> #<Foo:0x007fa3c5882b18 @a=1>
irb(main):010:0> foo2 = Foo.new(2)
=> #<Foo:0x007fa3c5878438 @a=2>
irb(main):011:0> p foo1.method1
1
=> 1
irb(main):012:0> p foo2.method1
2
=> 2
irb(main):013:0> a = Foo
=> Foo
irb(main):014:0> a.new(1)
=> #<Foo:0x007fa3c5853728 @a=1>
irb(main):015:0> Foo = 1
(irb):15: warning: already initialized constant Foo
(irb):1: warning: previous definition of Foo was here
=> 1
irb(main):016:0> p 1
1
=> 1
irb(main):017:0> class Hoge
irb(main):018:1> p 2
irb(main):019:1> end
2
=> 2
irb(main):020:0> p 3
3
=> 3
irb(main):001:0> class Foo
irb(main):002:1> def initialize(a)
irb(main):003:2> @a = a
irb(main):004:2> end
irb(main):005:1> def method1
irb(main):006:2> @a
irb(main):007:2> end
irb(main):008:1> end
=> :method1
irb(main):009:0> foo1 = Foo.new(1)
=> #<Foo:0x007fd159a3d810 @a=1>
irb(main):010:0> foo2 = Foo.new(2)
=> #<Foo:0x007fd159a34a80 @a=2>
irb(main):011:0> foo1.class == Foo
=> true
irb(main):012:0> foo2.class == Foo
=> true
irb(main):013:0> class FooExt < Foo
irb(main):014:1> def initialize(a,b)
irb(main):015:2> @b = b
irb(main):016:2> super a
irb(main):017:2> end
irb(main):018:1> def method2(c)
irb(main):019:2> @a + @b + c
irb(main):020:2> end
irb(main):021:1> end
=> :method2
irb(main):022:0> fooExt = FooExt.new(3, 4)
=> #<FooExt:0x007fd1599d5af8 @b=4, @a=3>
irb(main):023:0> p fooExt.method1
3
=> 3
irb(main):025:0> p fooExt.method2(5)
12
=> 12
irb(main):026:0> FooExt.superclass == Foo
=> true
irb(main):045:0> class Test2 < Foo
irb(main):046:1> def initialize(a,b)
irb(main):047:2> @b = b
irb(main):048:2> super
irb(main):049:2> end
irb(main):050:1> def method2(c)
irb(main):051:2> @a + @b + c
irb(main):052:2> end
irb(main):053:1> end
=> :method2
irb(main):054:0> test2 = Test2.new(3,4)
ArgumentError: wrong number of arguments (2 for 1)
    from (irb):2:in `initialize'
  from (irb):48:in `initialize'
  from (irb):54:in `new'
    from (irb):54
    from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):055:0> test2 = Test2.new(3)
ArgumentError: wrong number of arguments (1 for 2)
  from (irb):46:in `initialize'
  from (irb):55:in `new'
    from (irb):55
    from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):056:0> class Test3 < Foo
irb(main):057:1> def initialize(a)
irb(main):058:2> @b = a
irb(main):059:2> super
irb(main):060:2> end
irb(main):061:1> def method2(c)
irb(main):062:2> @a + @b + c
irb(main):063:2> end
irb(main):064:1> end
=> :method2
irb(main):065:0> test3 = Test3.new(4,4)
ArgumentError: wrong number of arguments (2 for 1)
  from (irb):57:in `initialize'
  from (irb):65:in `new'
    from (irb):65
    from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):066:0> class Test4 < Foo
irb(main):067:1> def initialize(a,b)
irb(main):068:2> @b = b
irb(main):069:2> super a
irb(main):070:2> end
irb(main):071:1> def method2(c)
irb(main):072:2> @a + @b + c
irb(main):073:2> end
irb(main):074:1> end
=> :method2
irb(main):075:0> test4 = Test4.new(3,4)
=> #<Test4:0x007fd159a57328 @b=4, @a=3>
irb(main):076:0> test4.method2(5)
=> 12
irb(main):077:0> class Test5 < Foo
irb(main):078:1> def initialize(a)
irb(main):079:2> super
irb(main):080:2> end
irb(main):081:1> def method2(c)
irb(main):082:2> @a + c
irb(main):083:2> end
irb(main):084:1> end
=> :method2
irb(main):085:0> test5 = Test5.new(3)
=> #<Test5:0x007fd159a15cc0 @a=3>
irb(main):086:0> test5.method2(4)
=> 7
irb(main):002:0> class Foo
irb(main):003:1> def initialize(a)
irb(main):004:2> @a = a
irb(main):005:2> end
irb(main):006:1> def method1
irb(main):007:2> @a
irb(main):008:2> end
irb(main):009:1> end
=> :method1
irb(main):010:0> class FooExt < Foo
irb(main):011:1> def initialize(a,b)
irb(main):012:2> @b = b
irb(main):013:2> super a
irb(main):014:2> end
irb(main):015:1> def method2(c)
irb(main):016:2> @a + @b + c
irb(main):017:2> end
irb(main):018:1> end
=> :method2
irb(main):019:0> foo1 = Foo.new(1)
=> #<Foo:0x007f7fd29f4b48 @a=1>
irb(main):020:0> p foo1.method2
NoMethodError: undefined method `method2' for #<Foo:0x007f7fd29f4b48 @a=1>
  from (irb):20
  from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):021:0> fooExt1 = FooExt.new(3,4)
=> #<FooExt:0x007f7fd29dc278 @b=4, @a=3>
irb(main):022:0> p fooExt1.method1
3
=> 3
irb(main):023:0> p Foo.ancestors
[Foo, Object, Kernel, BasicObject]
=> [Foo, Object, Kernel, BasicObject]
irb(main):024:0> p FooExt.ancestors
[FooExt, Foo, Object, Kernel, BasicObject]
=> [FooExt, Foo, Object, Kernel, BasicObject]
irb(main):025:0> Foo < Object
=> true
irb(main):026:0> Foo > Object
=> false
irb(main):027:0> Foo.instance_methods(false)
=> [:method1]
irb(main):028:0> FooExt.instance_methods(false)
=> [:method2]
irb(main):029:0> foo1.instance_variables
=> [:@a]
irb(main):031:0> FooExt.instance_variables
=> []
irb(main):032:0> fooExt = FooExt.new(3,4)
=> #<FooExt:0x007f7fd2951998 @b=4, @a=3>
irb(main):033:0> fooExt.instance_variables
=> [:@b, :@a]
irb(main):040:0> class Hoge
irb(main):041:1> def huga1; end
irb(main):042:1> def huga2; end
irb(main):043:1> alias :huga3 :huga1
irb(main):044:1> undef :huga2
irb(main):045:1> end
=> nil
irb(main):046:0> p Hoge.instance_methods(false)
[:huga1, :huga3]
=> [:huga1, :huga3]
irb(main):047:0> class Hoge
irb(main):048:1> def method_missing(m, *args)
irb(main):049:2> p "called:" + m.to_s
irb(main):050:2> super
irb(main):051:2> end
irb(main):052:1> end
=> :method_missing
irb(main):053:0> Hoge.new.no_method
"called:no_method"
NoMethodError: undefined method `no_method' for #<Hoge:0x007f7fd285d618>
  from (irb):50:in `method_missing'
  from (irb):53
  from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):061:0> class Hoge
irb(main):062:1> def huga1; end
irb(main):063:1> end
=> :huga1
irb(main):070:0> class Hoge
irb(main):071:1> def huga2; end
irb(main):072:1> alias :huga3 :huga1
irb(main):073:1> undef :huga2
irb(main):074:1> end
=> nil
irb(main):075:0> Hoge.instance_methods(false)
=> [:huga1, :huga3, :method_missing]
irb(main):076:0> class String
irb(main):077:1> def huga; 1; end
irb(main):078:1> end
=> :huga
irb(main):079:0> p "my string".huga
1
=> 1
irb(main):080:0> class Foo; end
=> nil
irb(main):081:0> class Bar; end
=> nil
irb(main):082:0> class Baz < Foo
irb(main):083:1> end
=> nil
irb(main):084:0> class Baz < Bar
irb(main):085:1> end
TypeError: superclass mismatch for class Baz
    from (irb):84
    from /Users/hirotoshi/.rbenv/versions/2.2.2/bin/irb:11:in `<main>'
irb(main):086:0> class Baz < Foo
irb(main):087:1> end
=> nil
irb(main):088:0> class Baz
irb(main):089:1> end
=> nil