Slides
Types and Conversions
Ref. WGR Chapter 7, Built-in essentials
Built-in Types
- Numbers
-
42(Fixnum) -
3.14159(Float)
-
- Booleans
truefalse
- Strings
"apple"'banana'
- Symbols
:apple
Built-in Types (cont.)
- Regular Expressions
/fo*/i
- Arrays
["apple", "banana"]
- Ranges
(1..10)
- Hashes
{:apple => 'red', :banana => 'yellow'}{apple: 'red', banana: 'yellow'}
to_*
-
to_iconverts to integer -
to_fconverts to float -
to_sconverts to string -
to_aconverts to array -
to_symconverts to symbol
to_s
- it's tempting to use
to_sin your UI - usually
to_sis insufficient and therefore useless - make a custom named method instead for your purposes
- e.g.
display_name
- e.g.
inspect
-
inspectproduces a debug-ready representation of the object - usually contains lots of information
- not to be shown to users, but good in log files or console
- a better way to debug: the "d" method