Trusted answers to developer questions

okwudili onyejiaku

The **volume** of an object is the total space occupied by that object. The volume of a cube is obtained by taking the product of three lengths of the cube.

volume = s * s * s

Syntax to obtain the volume of a cube in Ruby

: This represents the volume of the cube.**volume**

: This represents the length of each side of the cube.**s**

- The value returned is the volume of the cube.

# create function to get the volume of a cube def getVolume(lengthOfSide) volume = lengthOfSide * lengthOfSide * lengthOfSide puts "The volume of cube with length of side #{lengthOfSide} is = #{volume}" end # create length of sides of cubes l1 = 23 l2 = 5 l3 = 0.5 l4 = 100 # get the volumes of the cubes getVolume(l1) getVolume(l2) getVolume(l3) getVolume(l4)

Get the volume of a cube in Ruby

- Line 2: We create a function called
`getVolume()`

, which takes the length of the side of a cube, calculates the volume, and prints the result to the console. - Lines 9–12: We provide several values for the lengths of sides of different cubes whose volume we want to get.
- Lines 15–18: We invoke the function created to get the volume of the cubes.

RELATED TAGS

ruby

communitycreator

CONTRIBUTOR

okwudili onyejiaku

RELATED COURSES

View all Courses

Keep Exploring

Related Courses