Skip to content

Instantly share code, notes, and snippets.

@SoxFace
Last active October 10, 2019 10:20
Show Gist options
  • Select an option

  • Save SoxFace/adfc8bbfda8c4f06a6386a767af8c1d3 to your computer and use it in GitHub Desktop.

Select an option

Save SoxFace/adfc8bbfda8c4f06a6386a767af8c1d3 to your computer and use it in GitHub Desktop.

Revisions

  1. SoxFace revised this gist Oct 10, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions 2019OCT10.rb
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,7 @@ def example(a) # function a
    # 1) write the same thing but replace that logic using ruby each method
    # 2) write the same thing but avoid modifying the array, hint: use map
    # hint: (0..a).each { |i| ... } and (0..a).map { |i| ... }
    # https://www.rubyguides.com/2018/10/ruby-map-method/

    # 1) using each

  2. SoxFace revised this gist Oct 10, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion 2019OCT10.rb
    Original file line number Diff line number Diff line change
    @@ -38,5 +38,4 @@ def exampleEach(a)
    (0..a).map { |i|
    b[i] = i
    }
    return b
    end
  3. SoxFace revised this gist Oct 10, 2019. 1 changed file with 20 additions and 8 deletions.
    28 changes: 20 additions & 8 deletions 2019OCT10.rb
    Original file line number Diff line number Diff line change
    @@ -13,18 +13,30 @@ def example(a) # function a

    # => [0, 1, 2, 3, 4]

    # another way of doing it
    # again I'm unsure why b.push[i] is assigned to i
    # 1) write the same thing but replace that logic using ruby each method
    # 2) write the same thing but avoid modifying the array, hint: use map
    # hint: (0..a).each { |i| ... } and (0..a).map { |i| ... }

    def examplePush(a)
    # 1) using each

    def exampleEach(a)
    b = []

    for i in 0..a
    b.push[i] = i
    end
    (0..a).each { |i|
    b[i] = i
    }

    return b

    end

    example(5)

    # 2) using map

    def exampleEach(a)
    b = []

    (0..a).map { |i|
    b[i] = i
    }
    return b
    end
  4. SoxFace revised this gist Oct 10, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions 2019OCT10.rb
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,9 @@ def example(a) # function a

    # => [0, 1, 2, 3, 4]

    # another way of doing it
    # again I'm unsure why b.push[i] is assigned to i

    def examplePush(a)
    b = []

  5. SoxFace created this gist Oct 10, 2019.
    27 changes: 27 additions & 0 deletions 2019OCT10.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    def example(a) # function a
    b = [] # b is an empty array

    for i in 0..a # for every (i)ndex in the range 0 to a (or 4 in this example)
    b[i] = i # not clear how this works. I think it's a shorthand for .push
    # More importantly, I don't know why you have to assign the position of the index in the array to i
    end # end of loop

    return b #return b
    end # end of function

    example(4) # calls a, loops, pushes 0, 1, 2, 3, 4 into the array b

    # => [0, 1, 2, 3, 4]

    def examplePush(a)
    b = []

    for i in 0..a
    b.push[i] = i
    end

    return b
    end

    example(5)