Skip to content

Instantly share code, notes, and snippets.

@garyharan
Created May 6, 2013 13:51
Show Gist options
  • Save garyharan/5525294 to your computer and use it in GitHub Desktop.
Save garyharan/5525294 to your computer and use it in GitHub Desktop.
Just to show a few students a little more about objects
# encoding: UTF-8
#
# I realized objects were hard to grasp
# You already understood the building blocks of programs(String, Fixnumb, Array, Hash)
#
# Look at the following to see what you can learn:
s1 = 'String is an object'
s2 = String.new('String is an object')
puts "s1 = 'String is an object'"
puts "s2 = String.new('String is an object')"
puts "Is s1 == s2 true?"
puts "Yes!" if s1 == s2
puts "*" * 80
h1 = {
:mx => 'Mexico',
:fr => 'France',
:nl => 'Netherlands',
}
puts "h1 = {
:mx => 'Mexico',
:fr => 'France',
:nl => 'Netherlands',
}"
puts "h1.class == Hash ?"
puts "Yes!" if h1.class == Hash
h2 = Hash.new
h2.store(:mx, 'Mexico')
h2.store(:fr, 'France')
h2.store(:nl, 'Netherlands')
puts "
h2 = Hash.new
h2.store(:mx, 'Mexico')
h2.store(:fr, 'France')
h2.store(:nl, 'Netherlands')
"
puts "Is h1 == h2 true?"
puts "Yes!" if h1 == h2
puts "*" * 80
puts "
What does this tell you about the syntax?
\"Primitive\" types are also objects with methods associated to their instances.
Every time you affect an instance you're calling an internal method even if the syntax
does not make it immediately clear to you.
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment