<< Previous Page
    

    
      Table of Contents
    

    
      
    
  
Operator precedence

+-----+-------------------------------------------------+
| 1. | ! ~@ +@ |
| 2. | ** |
| 3. | -@ |
| 4. | * / % |
| 5. | + - |
| 6. | << >> |
| 7. | & |
| 8. | | ^ |
| 9. | > >= < <= |
| 10. | <=> == === != =~ !~ |
| 11. | && |
| 12. | || |
| 13. | .. ... |
| 14. | ? : |
| 15. | rescue |
| 16. | = **= *= /= %= += -= <<= >>= &= |= ^= &&= ||= |
| 17. | defined? |
| 18. | not |
| 19. | or and |
| 20. | if unless while until |
| 21. | { } do end |
+-----+-------------------------------------------------+
# Defining an unary operator
class String
def +@
"positive #{self}"
end
end

+"number"
=> "positive number"

# #super and #superclass
# Defining a binary operator
class String
def -(trash)
self.gsub(/#{trash}/, '')
end
end

"abcdef" - "cde"
=> "abf"
class Monster
def react(action)
:roar
end
end

class Komoju < Monster
def react(action)
if action == :pat
return :purr
end
super
end
end

# Get characters of string
'abc'.chars
=> ["a", "b", "c"]

# Get character codes of string
'abc'.chars.map(&:ord)
=> [97, 98, 99]
Komoju.new.react(:pat)
=> :purr

Komoju.new.react(:poke)
=> :roar

Komoju.superclass
=> Monster


# Modules
module LaserWeapon
def shoot_laser
'Pew pew pew!'
end
end

class Komoju
include LaserWeapon
end

Komoju.new.shoot_laser
=> 'Pew pew pew!'
    
      << Previous Page
    

    
      Table of Contents