Gerald Bauer
2018-11-03 08:35:42 UTC
Hello,
I've updated the (new) record library / gem [1] that lets you define
named tuples / records with typed structs / schemas. How?
Step 1: Define a (typed) struct for your records / named tuples. Example:
require 'record'
Beer = Record.define do
field :brewery ## note: default type is :string
field :city
field :name
field :abv, Float ## allows type specified as class (or use :float)
end
# or in "classic" style:
class Beer < Record::Base
field :brewery
field :city
field :name
field :abv, Float
end
And now use the new class to create new (typed) records. Example:
beer = Beer.new( 'Andechser Klosterbrauerei',
'Andechs',
'Doppelbock Dunkel',
'7%' )
# -or-
values = ['Andechser Klosterbrauerei', 'Andechs', 'Doppelbock Dunkel', '7%']
beer = Beer.new( values )
# -or-
beer = Beer.new( brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel',
abv: '7%' )
# -or-
hash = { brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel',
abv: '7%' }
beer = Beer.new( hash )
# -or-
beer = Beer.new
beer.update( brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel' )
beer.update( abv: 7.0 )
# -or-
beer = Beer.new
beer.parse( ['Andechser Klosterbrauerei', 'Andechs', 'Doppelbock
Dunkel', '7%'] )
# -or-
beer = Beer.new
beer.parse( 'Andechser Klosterbrauerei,Andechs,Doppelbock Dunkel,7%' )
# -or-
beer = Beer.new
beer.brewery = 'Andechser Klosterbrauerei'
beer.name = 'Doppelbock Dunkel'
beer.abv = 7.0
And so on. That's it.
Why? What for?
The (new) record library is "extracted" for easy (re)use from the
csvrecord library / gem [2] :-).
Cheers. Prost. Happy coding and data wrangling with ruby.
[1] https://github.com/rubycoco/record
[2] https://github.com/csvreader/csvrecord
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
I've updated the (new) record library / gem [1] that lets you define
named tuples / records with typed structs / schemas. How?
Step 1: Define a (typed) struct for your records / named tuples. Example:
require 'record'
Beer = Record.define do
field :brewery ## note: default type is :string
field :city
field :name
field :abv, Float ## allows type specified as class (or use :float)
end
# or in "classic" style:
class Beer < Record::Base
field :brewery
field :city
field :name
field :abv, Float
end
And now use the new class to create new (typed) records. Example:
beer = Beer.new( 'Andechser Klosterbrauerei',
'Andechs',
'Doppelbock Dunkel',
'7%' )
# -or-
values = ['Andechser Klosterbrauerei', 'Andechs', 'Doppelbock Dunkel', '7%']
beer = Beer.new( values )
# -or-
beer = Beer.new( brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel',
abv: '7%' )
# -or-
hash = { brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel',
abv: '7%' }
beer = Beer.new( hash )
# -or-
beer = Beer.new
beer.update( brewery: 'Andechser Klosterbrauerei',
city: 'Andechs',
name: 'Doppelbock Dunkel' )
beer.update( abv: 7.0 )
# -or-
beer = Beer.new
beer.parse( ['Andechser Klosterbrauerei', 'Andechs', 'Doppelbock
Dunkel', '7%'] )
# -or-
beer = Beer.new
beer.parse( 'Andechser Klosterbrauerei,Andechs,Doppelbock Dunkel,7%' )
# -or-
beer = Beer.new
beer.brewery = 'Andechser Klosterbrauerei'
beer.name = 'Doppelbock Dunkel'
beer.abv = 7.0
And so on. That's it.
Why? What for?
The (new) record library is "extracted" for easy (re)use from the
csvrecord library / gem [2] :-).
Cheers. Prost. Happy coding and data wrangling with ruby.
[1] https://github.com/rubycoco/record
[2] https://github.com/csvreader/csvrecord
Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>