Skip to content

Instantly share code, notes, and snippets.

@pokotyamu
Last active April 19, 2016 15:56
Show Gist options
  • Save pokotyamu/fe0ca8bd40aa0cf167af37842eca4de6 to your computer and use it in GitHub Desktop.
Save pokotyamu/fe0ca8bd40aa0cf167af37842eca4de6 to your computer and use it in GitHub Desktop.
到達度テストversion2

問1

def map(array,&block)
  new_array = []
  array.each do |item|
    new_array << block.call(item)
  end
  new_array
end

array_block = Proc.new { | item | item.to_i}
array = [1,2,3]
result = map(array,&array_block)
p result

問2

module Hoge
  def hoge
    p "hoge method"
  end
  module_function :hoge
end

class Fuga
  include Hoge
  Hoge.hoge
end

g = Fuga.new

問3

hoge #=> "NilClass"
hoge {} #=> "Proc"

引数なしはnil {}でProcとして渡される。

def hoge(&block)
  block.call
end
hoge{p "fuga"}

問4

出題内容が理解できなかった。

問5

module A
end
class B
  include A
end
value = B.new
x = B.new

p value.class===x.class
p value.class==x.class
p x.class

=begin
value = String.new
x = String.new
=end

case value
when x
  p "x"
else
  p "other"
end
if value.is_a?(x.class)
  p "x"
else
  p "other"
end
  • 等しくなる時
    • x:
    • value:
  • ならない時
    • x: クラスBのインスタンス
    • value: クラスBのインスタンス

等しくなる時、ならない時の違いがいまいち納得出来ていない。

問6

def change_instance_variables(&block)
  begin
    @b = "c"
    block.call
    @b = "b"
  rescue
    @b = "b"
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment