Skip to content

Instantly share code, notes, and snippets.

@Jbot29
Created September 11, 2014 00:35
Show Gist options
  • Select an option

  • Save Jbot29/6f4c9bf0657a9ec37cbf to your computer and use it in GitHub Desktop.

Select an option

Save Jbot29/6f4c9bf0657a9ec37cbf to your computer and use it in GitHub Desktop.
Yield Times each example
class MyFixNum
attr_accessor :num
def times
for x in 0..@num
yield x #if block_given?
end
end
end
class MyArray
attr_accessor :array
def each
for x in 0..@array.length
yield @array[x]
end
end
end
IRB
2.1.2 :016 > load 'yield.rb'
=> true
2.1.2 :017 >
2.1.2 :018 >
2.1.2 :019 > new_num = MyFixNum.new
=> #<MyFixNum:0x007ffabb015f50>
2.1.2 :020 > new_num.num = 5
=> 5
2.1.2 :021 > new_num
=> #<MyFixNum:0x007ffabb015f50 @num=5>
2.1.2 :022 > new_num.times do |x|
2.1.2 :023 > puts x
2.1.2 :024?> end
0
1
2
3
4
5
=> 0..5
2.1.2 :025 > my_array = MyArray.new
=> #<MyArray:0x007ffaba8b8ca8>
2.1.2 :026 > my_array.array = ["Test","test2"]
=> ["Test", "test2"]
2.1.2 :027 > my_array.each do |x|
2.1.2 :028 > puts x
2.1.2 :029?> end
Test
test2
=> 0..2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment