How to Replicate the Rails image_tag Method in Ruby


This exercise asks you to replicate the functionality of the popular image_tag method provided by the Ruby on Rails framework.


Build a program that replicates the functionality of the image_tag method provided by the Ruby on Rails framework.

Exercise File

Code File

Exercise Description

The method should be able to dynamically generate an HTML image tag. Specifically it needs to be able to take in any number of arguments and integrate the arguments and their values as elements contained in the generated image tag.


@image_path = ""
image_tag(@image_path)) # "<img src=''>"
image_tag(@image_path, width: 42)) # "<img src='' width='42'>"
image_tag(@image_path, alt: "My Image") # "<img src='' width='42' alt='My Image'>"

Real World Usage

This process is utilized extensively when it comes to building view helper methods in Rails applications. If you work in Rails based applications you will encounter many situations when you need to dynamically generate HTML code.


Can be found on the solutions branch on github.


Please enter your comment!
Please enter your name here