I am having trouble understanding how characters work in my code. I understand that they are essentially immutable strings, but I donβt quite understand how characters automatically recognize other parts of my code.
For example, in the program below, I pass two object methods into my math_machine methods, but for this I use a character representing their name. How does Ruby know what I mean by these methods?
def plus x,y return x+y end def minus x,y return xy end def math_machine(code,x,y) code.call(x,y) end puts math_machine(method(:plus),5,5) puts math_machine(method(:minus),5,5)
Another example of characters that I donβt understand is about encapsulation - how attr_reader , attr_writer and attr_accessor know that the next character refers to an instance variable in my program?
If someone can explain to me the mysterious nature of characters in Ruby (what happens behind the scenes), that would be awesome!
Kvass source share