Can we override operators in Ruby? -
in case...such as
(10 + 20) * 10 => 300
the expression fragment enclosed in parentheses evaluated before higher precedence multiplication.so applies other operators well.can override other operators such << !
...etc
when humans evaluate expressions, starting @ left of expression , working towards right. example, working left right result of 300 following expression:
10 + 20 * 10 = 300
this because we, humans, add 10
20
, resulting in 30
, multiply 10
arrive @ 300
. ask ruby perform same calculation , different answer:
> 10 + 20 * 10 => 210
this direct result of operator precedence. ruby has set of rules tell in order operators should evaluated in expression. clearly, ruby considers multiplication operator (*) of higher precedence addition (+) operator.
overriding operator precedence
the precedence built ruby can overridden surrounding lower priority section of expression parentheses. example:
> (10 + 20) * 10 => 300
in above example, expression fragment enclosed in parentheses evaluated before higher precedence multiplication.
for more info refer :
i hope makes clear understand :)
Comments
Post a Comment