Define a method to convert Cartesian (x,y) coordinates to Polar : Method Creation « Method « Ruby






Define a method to convert Cartesian (x,y) coordinates to Polar


def polar(x,y)
  theta = Math.atan2(y,x)   # Compute the angle
  r = Math.hypot(x,y)       # Compute the distance
  [r, theta]                # The last expression is the return value
end

distance, angle = polar(2,2)

 








Related examples in the same category

1.Creating and Calling a Method
2.A simple definition for a method named hello, created with the keywords def and end
3.define methods that have arguments
4.You can a function with or without parenthesis
5.Call a function for a parameter with or without parenthesis
6.You can call another function based on the return from this funtion call
7.method with ?
8.Add a new method by add method to Class