Your SlideShare is downloading. ×
0
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Ruby struct
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Ruby struct

2,055

Published on

Using Ruby's Struct Class.

Using Ruby's Struct Class.

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,055
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Ruby’s Struct Class typdef struct { int id; char *name; } Awesome; Ken Collins metaskills.netMonday, November 14, 11
  • 2. A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.Monday, November 14, 11
  • 3. Ad Hoc Example Language = Struct.new(:name, :class_based) ruby = Language.new "Ruby", true js = Language.new "JavaScript", false objc = Language.new "Objective-C" ruby # => #<struct Language name="Ruby", class_based=true> js # => #<struct Language name="JavaScript", class_based=false> ruby.class_based # => true js.class_based # => false objc.class_based = true objc.class_based # => trueMonday, November 14, 11
  • 4. Sub Class Example class MyObject < Struct.new(:id, :name, :key) end o = MyObject.new # => #<struct MyObject id=nil, name=nil, key=nil> o.id # => nil o.name # => nil o.id = 12345 o.name = "Test Object" o # => #<struct MyObject id=12345, name="Test Object", key=nil>Monday, November 14, 11
  • 5. But Why Structs? class MyObject attr_accessor :id, :name, :key def initialize(*args) @id, @name, @key = args end end o = MyObject.new 12345, "Test Object" o.id # => 12345 o.name # => "Test Object"Monday, November 14, 11
  • 6. But Why Structs?Monday, November 14, 11
  • 7. But Why Structs? Convenient Alternative.Monday, November 14, 11
  • 8. But Why Structs? Convenient Alternative. Attribute Methods With [] & []= Can Be Powerful. Not ivar based.Monday, November 14, 11
  • 9. But Why Structs? Convenient Alternative. Attribute Methods With [] & []= Can Be Powerful. Not ivar based. Easy Enumeration Of Attribute Data.Monday, November 14, 11
  • 10. But Why Structs? Convenient Alternative. Attribute Methods With [] & []= Can Be Powerful. Not ivar based. Easy Enumeration Of Attribute Data. Default Equality On All Attributes.Monday, November 14, 11
  • 11. Attribute Accessors class Person < Struct.new(:name) def name parts = self[:name].to_s.split( ) capped_parts = parts.map { |x| x.capitalize } capped_parts.join( ) end end p = Person.new "KEN coLLins" p.name # => "Ken Collins" p[:name] = mETAskiLLs p.name # => "Metaskills"Monday, November 14, 11
  • 12. Easy Enumeration Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joe.each { |x| x } # >> Joe Smith # >> 123 Maple, Anytown NC # >> 12345 joe.each_pair { |name, value| puts("#{name} => #{value}") } # >> name => Joe Smith # >> address => 123 Maple, Anytown NC # >> zip => 12345Monday, November 14, 11
  • 13. Default Attribute Equality Customer = Struct.new(:name, :address, :zip) joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) joejr = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345) jane = Customer.new("Jane Doe", "456 Elm, Anytown NC", 12345) joe == joejr #=> true joe == jane #=> falseMonday, November 14, 11
  • 14. Thanks! Ken Collins metaskills.netMonday, November 14, 11

×