Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby Struct
Search
Ken Collins
April 11, 2012
2
530
Ruby Struct
Using Ruby’s Struct Class.
Ken Collins
April 11, 2012
Tweet
Share
More Decks by Ken Collins
See All by Ken Collins
Real-World AI Automation with Lambda & Bedrock
metaskills
0
31
The Lambda Sidecar Pattern for Event-Driven Kubernetes
metaskills
0
220
The Case for Rails on Lambda v1
metaskills
0
510
Learn to Program The Cloud with Ruby & AWS Lambda
metaskills
0
58
Full-Stack to Functions & Back Again
metaskills
0
200
AWS Lambda & Ruby/Rails with SAM
metaskills
1
4.2k
Turbo Applications - Winning with Ajax + pushState + Rails
metaskills
0
250
Modular CSS & Sass
metaskills
1
150
Like A Ross! - Building The CustomInk.com Living Style Guide
metaskills
0
450
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
648
58k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
25
2.3k
How to train your dragon (web standard)
notwaldorf
73
5.2k
Bootstrapping a Software Product
garrettdimon
PRO
302
110k
Building a Scalable Design System with Sketch
lauravandoore
456
32k
How To Stay Up To Date on Web Technology
chriscoyier
782
250k
Put a Button on it: Removing Barriers to Going Fast.
kastner
58
3k
Infographics Made Easy
chrislema
238
18k
Designing for Performance
lara
601
67k
Visualization
eitanlees
136
14k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
78
42k
Transcript
Ruby’s Struct Class Ken Collins metaskills.net typdef struct { int
id; char *name; } Awesome; Monday, November 14, 11
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
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 # => true Monday, November 14, 11
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
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
But Why Structs? Monday, November 14, 11
But Why Structs? Convenient Alternative. Monday, November 14, 11
But Why Structs? Convenient Alternative. Attribute Methods With [] &
[]= Can Be Powerful. Not ivar based. Monday, November 14, 11
But Why Structs? Convenient Alternative. Attribute Methods With [] &
[]= Can Be Powerful. Not ivar based. Easy Enumeration Of Attribute Data. Monday, November 14, 11
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
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
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 => 12345 Monday, November 14, 11
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 #=> false Monday, November 14, 11
Thanks! Ken Collins metaskills.net Monday, November 14, 11