How to Replicate the Rails image_tag Method in Ruby

0
615

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

Summary

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.

Examples

@image_path = "https://devcamp.com/some_pic.jpg"
image_tag(@image_path)) # "<img src='https://devcamp.com/some_pic.jpg'>"
image_tag(@image_path, width: 42)) # "<img src='https://devcamp.com/some_pic.jpg' width='42'>"
image_tag(@image_path, alt: "My Image") # "<img src='https://devcamp.com/some_pic.jpg' 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.

Solution

Can be found on the solutions branch on github.

LEAVE A REPLY

Please enter your comment!
Please enter your name here