Some tips in Ruby

Tram Ho

Some tips in ruby

  1. Assigns the remaining elements of the array
  2. Change default values ​​in hash
  3. Raise an exception in the hash
  4. Operator & when comparing arrays
  5. Assign multiple values ​​to multiple variables on the same line
  6. Use | to get all values ​​from two arrays
  7. Use – to return an element that does not exist in the array
  8. Swap values
  9. Use !! when searching in RegEx
  10. Conclusion

1. Assign the remaining elements of the array

  • In the structure of the array we can use * to assign the rest of the array instead of entering values ​​individually

For example:

2. Change the default value in hash

  • We can use default and default_proc to change the default value in the hash

Example 1:

Example 2:

3. Raise an exception in the hash

  • Raises an exception when the key in the hash does not exist

For example:

Share the news now

Source : Viblo