https://github.com/abargnesi/book_basic
https://github.com/abargnesi/book_basic
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/abargnesi/book_basic
- Owner: abargnesi
- Created: 2015-06-08T14:37:04.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-09T14:48:38.000Z (almost 10 years ago)
- Last Synced: 2025-01-12T17:47:37.510Z (5 months ago)
- Size: 188 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Ruby introduction
## Sending a message
Ruby objects are mutable. Objects can respond to messages (e.g. methods), in a dynamic fashion, and you can change this at run-time.
Let us take the following:
```ruby
my_obj = Object.new
# can I respond to the foo message?
my_obj.respond_to?(:foo)
# => false# add foo message handler to my_obj
class << my_objdef foo
:foo
end
end# how about now?
my_obj.respond_to?(:foo)
# => true# send the foo message to my_obj
my_obj.send(:foo)
# => :foo# send the foo message using dot notation
my_obj.foo
# => :foo
```This example shows how we can add an instance method to the `my_obj` object at run-time.