Ruby Literals

literals create object that can be used in program.

  • String
  • Number
  • Symbol
  • Boolean & nil
  • Array
  • Range
  • Hash
  • RegExp
  • Procs

Number

you can use underscore _ to enhance human readability

for example

t = 1_2_3_4
s = 1234

s == t      #==> true

Boolean & nil

except false and nil are false, all others are evaluated to true

Boolean Method

a function defined ends with ?

def ok?
    # codes chunk
end

then, they will always return either true or false

for more, please read Ruby-Boolean

Range

(1..100)    # includes end value, 100
(1...100)   # excludes end value, 100

Hash

using key-value method

{"a" => 1, "b" => 2}

using symbol keys

{a:1, b:2}

OR

{"a":1, "b":2}

Regular Expression

anything included between /

Percent Inputs

%() creates String, besides any other type of characters are supported like, %[], %{}, %!!, %//

  • %Q double quoted String, similar like top, supports expression substitution or escapes.

  • %q single quoted String

  • %W double quoted Strings in Array

  • %w single quoted Strings in Array

  • %x like \`, command line interpreter, note: the newline char will be appended

%x(echo "good")     #==> "good\n"   ==> '\n' will be preserved
  • %r regular expression

  • %s single symbol, for example, %s(how are you) will output :"how are you"

  • %I double quoted symbols in Array

  • %i single quoted symbols in Array

Here Document

please refer to ruby-heredoc