Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created March 30, 2017 06:49
Show Gist options
  • Save bparanj/6458917961a11c376048e7ae9baf669d to your computer and use it in GitHub Desktop.
Save bparanj/6458917961a11c376048e7ae9baf669d to your computer and use it in GitHub Desktop.
Ruby Object Model Exercise #2
sum = 0
i = 1
while i < 5
sum += i
i += 1
end
p sum
@bparanj
Copy link
Author

bparanj commented Mar 30, 2017

sum := 0.
i := 1.
[i < 5]
    whileTrue:
      sum += i.
      i := i + 1].

Implement message centric Ruby equivalent.

@bparanj
Copy link
Author

bparanj commented Apr 1, 2017

sum = 0
i = 1

# Some code here
# The condition variable is a Proc object.

condition.while_true do 
  sum += i
  i += 1
end
p sum

@tahngarth825
Copy link

tahngarth825 commented Apr 1, 2017

sum = 0
i = 1

class Proc
  def while_true
    yield while self.call
  end
end

condition = Proc.new{ i < 5 }

condition.while_true do
  sum += i
  i += 1
end
puts sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment