recently during a presentation, when I start the console Ruby somewhat surprised - after defining any function in the interpreter such as this:
irb (main): 001:0> def function
irb (main): 002:1 > puts "Hello World"
irb (main): 003:1> end
=> Nil irb
(main): 004:0>
unless each facility has been able to call this function on his behalf. Was surprised to see that it works for example, such a thing:
irb (main): 004:0> "Hello World 5.funkcja
=> nil
also
irb (main): 005:0> "a piece of text." Hello World function
=> nil
Rummaging little documentation and googlach eventually all became clear. And so one after another: When you open the interpreter in a kind of object that created the interpreter. You can check this by typing the command line:
irb (main): 006:0> self
=> main
irb (main): 007:0>
Moreover, the very inform the interpreter's prompt 'irb ( main): 007:0> '- but I do not think I would have fallen that it is in main building. Sam
main object is of type Object:
irb (main): 007:0> self.class
=> Object
Now, if you will list all the methods defined for the main building where you will find our function, inter alia:
irb (main): 008:0> self.methods
=> ["inspect", "pushws", "cwws", "clone", "cb", "help",
...
"irb_print_working_binding", "===", "workspaces", "instance_variable_set", "irb_pop_binding",
"kind_of?", "extend", "irb_chws", "to_s", "irb_pushws", "irb_require", "irb_pwws
", "hash", "class", "irb_cb", "include", "irb_context", "tainted?", "=~", "priva
te_methods", "irb_pop_workspace", "irb_cwb", "irb_jobs", "nil?", "funkcja" , "unt
aint", "irb_bindings", "exit", "irb_current_working_workspace", "irb_popb", "irb
_cws", "is_a?", "Fg"]
5 (Fixnum) and "string" (String) Object inherit what can you look like this:
irb (main): 009:0> 5.is_a? Object
=> true
irb (main): 010:0> "string". Is_a? Object
=> true
therefore, such a proposal: the class object by modifying the object and placing it in any way - it becomes instantly available to all child classes of objects. Interesting ...
0 comments:
Post a Comment