In this Ruby coding exercise your knowledge of blocks will be tested. The requirement is to build a User class that can be created and passed a block to set its values in addition to the traditional process for setting an object’s data.
Summary
Build a User Class that Can Be Passed a Block to Set Its Values.
Exercise File
Exercise Description
In order to properly pass this coding exercise you’ll need to create a Ruby User
class that is flexible enough to take a block along with the traditional data setting process..
Examples
user = User.new do |u| u.name = "Jordan" u.email = "[email protected]" end user.name # 'Jordan' user.email # '[email protected]'
Should work, along with:
user = User.new user.name = "Jordan" user.email = "[email protected]" user.name # 'Jordan' user.email # '[email protected]'
Real World Usage
Using blocks in Ruby is important when it comes to creating flexible interfaces for methods and classes. Additionally, a large number of Ruby methods optionally take a block, so it’s helpful to understand how the process works.
Solution
Can be found on the solutions branch on github.