To take the sine of a number in Ruby, you can use the Math.sin
method, which is part of Ruby’s standard library. Here’s an example:
main.rb40 chars4 lines
In this example, we assign the value of 3.14
to the variable x
, then call the Math.sin
method on x
and assign the result to the variable sin_x
. Finally, we use puts
to print the value of sin_x
to the console.
The Math.sin
method takes an argument in radians and returns the sine of that angle. Note that Math.sin
returns a float, so the result will be a decimal value.
You can also use the trigonometry methods Math.cos
and Math.tan
to find the cosine and tangent of an angle, respectively.
gistlibby LogSnag