January 24 - Build a User Class that Can Be Passed a Block to Set Its Values
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.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
  • Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license

Summary

Build a User Class that Can Be Passed a Block to Set Its Values.

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 = "test@test.com"
end

user.name # 'Jordan'
user.email # 'test@test.com'

Should work, along with:

user = User.new
user.name = "Jordan"
user.email = "test@test.com"

user.name # 'Jordan'
user.email # 'test@test.com'

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.

Test Cases

Code File