diff --git a/open_struct.rb b/open_struct.rb new file mode 100644 index 0000000..9c84d5d --- /dev/null +++ b/open_struct.rb @@ -0,0 +1,9 @@ +# open_struct.rb + +require 'ostruct' + +p = OpenStruct.new +p.name = "Karthik" +p.age = 30 +puts "Hello, I am #{p.name}, age #{p.age}" + diff --git a/struct_about_me.rb b/struct_about_me.rb new file mode 100644 index 0000000..da9b549 --- /dev/null +++ b/struct_about_me.rb @@ -0,0 +1,10 @@ +# struct_about_me.rb + +person = Struct.new :name, :age do + def about_me + "Hello, I am #{self.name}, age #{self.age}" + end +end +p = person.new "Karthik", 30 +puts p.about_me + diff --git a/struct_about_me_1.rb b/struct_about_me_1.rb new file mode 100644 index 0000000..cb1ea68 --- /dev/null +++ b/struct_about_me_1.rb @@ -0,0 +1,8 @@ +person = Struct.new :name, :age do + def about_me + "Hello, I am #{@name}, age #{@age}" + end +end +p = person.new "Karthik", 30 +puts p.about_me + diff --git a/struct_about_me_2.rb b/struct_about_me_2.rb new file mode 100644 index 0000000..0abe6a3 --- /dev/null +++ b/struct_about_me_2.rb @@ -0,0 +1,8 @@ +person = Struct.new :name, :age do + def about_me + "Hello, I am #{person.name}, age #{person.age}" + end +end +p = person.new "Karthik", 30 +puts p.about_me + diff --git a/struct_constant.rb b/struct_constant.rb new file mode 100644 index 0000000..f3081e3 --- /dev/null +++ b/struct_constant.rb @@ -0,0 +1,9 @@ +# struct_constant.rb + +Person = Struct.new :name, :age +p = Person.new +p.name = "Karthik" +p.age = 30 + +puts "Hello, I am #{p.name}, age #{p.age}" + diff --git a/struct_one_line.rb b/struct_one_line.rb new file mode 100644 index 0000000..bafec11 --- /dev/null +++ b/struct_one_line.rb @@ -0,0 +1,7 @@ +# struct_one_line.rb + +person = Struct.new :name, :age +p = person.new "Karthik", 30 + +puts "Hello, I am #{p.name}, age #{p.age}" + diff --git a/struct_start.rb b/struct_start.rb new file mode 100644 index 0000000..bea5118 --- /dev/null +++ b/struct_start.rb @@ -0,0 +1,9 @@ +# struc_start.rb + +person = Struct.new :name, :age +p = person.new +p.name = "Karthik" +p.age = 30 + +puts "Hello, I am #{p.name}, age #{p.age}" + diff --git a/struct_wrong.rb b/struct_wrong.rb new file mode 100644 index 0000000..76981bb --- /dev/null +++ b/struct_wrong.rb @@ -0,0 +1,10 @@ +# struct_wrong.rb + +person = Struct.new :name, :age +p = person.new +p.name = "Karthik" +p.age = 30 +p.profession = "Engineer" + +puts "Hello, I am #{p.name}, age #{p.age}, and I am on a #{p.profession}" +