Basic Ruby [Part 2] Hash, Symbol, Range, Constant

5. Hashes

>> person = ['Kevin', "Skoglund", 'male', 'blue', 'blonde']
=> ["Kevin", "Skoglund", "male", "blue", "blonde"]
>> person = {'first_name' => 'Kevin', 'last_name' => 'Skoglund' }
=> {"first_name"=>"Kevin", "last_name"=>"Skoglund"}
>> person['first_name']
=> "Kevin"
>> person['last_name']
=> "Skoglund"
>> person.index('Kevin')
=> "first_name"
>> mixed = { 1 => ['a', 'b', 'c'], 'hello' => 'world', [10,20] => 'top'}
=> {1=>["a", "b", "c"], "hello"=>"world", [10, 20]=>"top"}
>> mixed[1]
=> ["a", "b", "c"]
>> mixed[[10,20]]
=> "top"
>> mixed.keys
=> [1, "hello", [10, 20]]
>> mixed.values
=> [["a", "b", "c"], "world", "top"]
>> mixed.length
=> 3
>> mixed.size
=> 3
>> mixed.to_a
=> [[1, ["a", "b", "c"]], ["hello", "world"], [[10, 20], "top"]]
>> mixed.clear
=> {}
>> mixed = {}
=> {}
>> persen
=> {"first_name"=>"Kevin", "last_name"=>"Skoglund"}
>> person['gender'] = 'male'
=> "male"
>> person
=> {"first_name"=>"Kevin", "last_name"=>"Skoglund", "gender"=>"male"}

6. Symbols

>> :test
=> :test
>> :this_test
=> :this_test
>> "test".object_id
=> 70160342646100
>> :test.object_id
=> 355868
>> "test".object_id
=> 70160342612580
>> :test.object_id
=> 355868
>> hash = {:first_name => 'Kevin', :last_name => 'Skoglund'}
=> {:first_name=>"Kevin", :last_name=>"Skoglund"}
>> hash['first_name']
=> nil
>> hash[:first_name]
=> "Kevin"

7. Boolean

>> x = 1
=> 1
>> x == 1
=> true
>> true.class
=> TrueClass
>> false.class
=> FalseClass
>> x != 1
=> false
>> x < 3
=> true
>> x > 3
=> false
>> !x
=> false
>> !y
NameError: undefined local variable or method `y' for main:Object
from (irb):9
from /Users/Capuchino/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'
>> y = false
=> false
>> !y
=> true
>> 1 <= 4 && 5 <= 100
=> true
>> 1 <= 4 && 5 <= 100 && 100 >= 200
=> false
>> 1 <= 4 || 5 <= 100 || 100 >= 200
=> true
>> 16 <= 4 || 500 <= 100 || 100 >= 200
=> false
>> x.nil?
=> false
>> y.nil?
=> false
>> z = nil
=> nil
>> z.nil?
=> true
>> 2.between?(1,4)
=> true
>> 2.between?(3,4)
=> false
>> [1,2,3].empty?
=> false
>> [].empty?
=> true
>> [1,2,3].include?(2)
=> true
>> [1,2,3].include?(5)
=> false
>> {'a' => 1, 'b' => 2}.has_key?('a')
=> true
>> {'a' => 1, 'b' => 2}.has_key?(:a)
=> false
>> {'a' => 1, 'b' => 2}.has_value?(2)
=> true
>> defined?(a)
=> nil
>> a = 10
=> 10
>> defined?(a)
=> "local-variable"

8. Range

>> #inclussive range
?> 1..10
=> 1..10
>> x = 1..10
=> 1..10
>> x.class
=> Range
>> 1..10.class
ArgumentError: bad value for range
from (irb):39
from /Users/Capuchino/.rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'
>> (1..10).class
=> Range
>> x.begin
=> 1
>> x.end
=> 10
>> x.first
=> 1
>> x.last
=> 10
>> #exclusive range
?> y = 1...10
=> 1...10
>> y.begin
=> 1
>> y.end
=> 10
>> y.include?(1)
=> true
>> y.include?(10)
=> false
>> x.include?(1)
=> true
>> z = [*x]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> x
=> 1..10
>> z
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>> 'a'..'m'
=> "a".."m"
>> alpha = 'a'..'m'
=> "a".."m"
>> alpha.include?('g')
=> true
>> [*alpha]
=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"]

9. Constants

>> test = 1
=> 1
>> TEST = 2
=> 2
>> test
=> 1
>> TEST
=> 2
>> Hello = 10
=> 10
>> test = 100
=> 100
>> TEST = 100
(irb):73: warning: already initialized constant TEST
(irb):68: warning: previous definition of TEST was here
=> 100
>> TEST
=> 100
>> Hello = 20
(irb):75: warning: already initialized constant Hello
(irb):71: warning: previous definition of Hello was here
=> 20