How can I implement options hashes? How is the structure of a class that has option hashes in it? Say I have a person class. I want to implement a method such as my_age that when called upon will tell me my age using options hashes.
相关问题
- facebook error invalid key hash for some devices
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
相关文章
- Ruby using wrong version of openssl
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
- Bcrypt vs Hash in laravel
- uninitialized constant Mysql2::Client::SECURE_CONN
Might be worth mentioning that Ruby 2.1 adds the ability to pass in keyword arguments that don't need to be in a particular order AND you can make them be required or have default values.
Ditching the options hash reduces the boilerplate code to extract hash options. Unnecessary boilerplate code increases the opportunity for typos and bugs.
Also with keyword arguments defined in the method signature itself, you can immediately discover the names of the arguments without having to read the body of the method.
Required arguments are followed by a colon while args with defaults are passed in the signature as you'd expect.
For example:
In Ruby 2.x you can use
**
operator:You could do something like this:
and now you're able to call to the age like this