Difference between revisions of "Ruby-marc"
From Code4Lib
(New page: Rdocs are here: [http://marc.rubyforge.org http://marc.rubyforge.org] It can be useful to play around with a library in irb before you try to write a real app with it. Here are some irb e...) |
|||
Line 2: | Line 2: | ||
It can be useful to play around with a library in irb before you try to write a real app with it. Here are some irb examples. If you need a file of MARC records to work with, you can download one from here: [http://www.archive.org/search.php?query=mediatype%3A%22data%22%20MARC http://www.archive.org/search.php?query=mediatype%3A%22data%22%20MARC]. | It can be useful to play around with a library in irb before you try to write a real app with it. Here are some irb examples. If you need a file of MARC records to work with, you can download one from here: [http://www.archive.org/search.php?query=mediatype%3A%22data%22%20MARC http://www.archive.org/search.php?query=mediatype%3A%22data%22%20MARC]. | ||
+ | |||
+ | |||
+ | ==== Recipe: read in a large file of marc records, pull out one of them and write it to a separate file ==== | ||
+ | <pre>paz:blacklight eos8d$ irb | ||
+ | irb(main):001:0> require 'rubygems' | ||
+ | => true | ||
+ | irb(main):002:0> require 'marc' | ||
+ | => true | ||
+ | irb(main):012:0> reader = MARC::Reader.new('/usr/local/projects/bl-demo/data/lc_records.utf8.mrc') | ||
+ | => #<MARC::Reader:0x123dec0 @handle=#<File:/usr/local/projects/bl-demo/data/lc_records.utf8.mrc>> | ||
+ | irb(main):014:0> record = reader.first | ||
+ | => #<MARC::Record:0x1236c10 @leader="00990cam a22002894a 4500", @fields=[#<MARC::ControlField:0x1236594 @tag="001", @value=" 00008195 ">, #<MARC::ControlField:0x12364cc @tag="003", @value="DLC">, #<MARC::ControlField:0x1236404 @tag="005", @value="20090126093013.0">, #<MARC::ControlField:0x123633c @tag="008", @value="000128s2000 azuab b s000 0beng ">, #<MARC::DataField:0x1236274 @tag="010", @subfields=[#<MARC::Subfield:0x12360bc @value=" 00008195 ", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1236058 @tag="020", @subfields=[#<MARC::Subfield:0x1235eb4 @value="0816520682 (cloth : acid-free paper)", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1235e50 @tag="020", @subfields=[#<MARC::Subfield:0x1235cac @value="0816520690 (pbk. : acid-free paper)", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1235c48 @tag="040", @subfields=[#<MARC::Subfield:0x1235a90 @value="DLC", @code="a">, #<MARC::Subfield:0x1235a54 @value="DLC", @code="c">, #<MARC::Subfield:0x1235a18 @value="DLC", @code="d">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12359b4 @tag="042", @subfields=[#<MARC::Subfield:0x1235824 @value="pcc", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12357c0 @tag="043", @subfields=[#<MARC::Subfield:0x123561c @value="n-us-ct", @code="a">, #<MARC::Subfield:0x12355e0 @value="n-us---", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x123557c @tag="050", @subfields=[#<MARC::Subfield:0x12353d8 @value="E99.M83", @code="a">, #<MARC::Subfield:0x123539c @value="T364 2000", @code="b">], @indicator2="0", @indicator1="0">, #<MARC::DataField:0x1235338 @tag="082", @subfields=[#<MARC::Subfield:0x123516c @value="974.7/004973", @code="a">, #<MARC::Subfield:0x1235130 @value="B", @code="a">, #<MARC::Subfield:0x12350f4 @value="21", @code="2">], @indicator2="0", @indicator1="0">, #<MARC::DataField:0x1235090 @tag="100", @subfields=[#<MARC::Subfield:0x1234ed8 @value="Zobel, Melissa Tantaquidgeon,", @code="a">, #<MARC::Subfield:0x1234e9c @value="1960-", @code="d">], @indicator2=" ", @indicator1="1">, #<MARC::DataField:0x1234e38 @tag="245", @subfields=[#<MARC::Subfield:0x1234c6c @value="Medicine trail :", @code="a">, #<MARC::Subfield:0x1234c1c @value="the life and lessons of Gladys Tantaquidgeon /", @code="b">, #<MARC::Subfield:0x1234bcc @value="Melissa Jayne Fawcett.", @code="c">], @indicator2="0", @indicator1="1">, #<MARC::DataField:0x1234b68 @tag="260", @subfields=[#<MARC::Subfield:0x12349b0 @value="Tucson :", @code="a">, #<MARC::Subfield:0x1234960 @value="University of Arizona Press,", @code="b">, #<MARC::Subfield:0x1234924 @value="c2000.", @code="c">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12348c0 @tag="300", @subfields=[#<MARC::Subfield:0x12346f4 @value="xvi, 177 p. :", @code="a">, #<MARC::Subfield:0x12346a4 @value="ill., map ;", @code="b">, #<MARC::Subfield:0x1234668 @value="21 cm.", @code="c">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1234604 @tag="504", @subfields=[#<MARC::Subfield:0x1234460 @value="Includes bibliographical references (p. 177).", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12343fc @tag="600", @subfields=[#<MARC::Subfield:0x1234258 @value="Tantaquidgeon, Gladys.", @code="a">], @indicator2="0", @indicator1="1">, #<MARC::DataField:0x12341f4 @tag="650", @subfields=[#<MARC::Subfield:0x123403c @value="Mohegan Indians", @code="a">, #<MARC::Subfield:0x1234000 @value="Biography.", @code="v">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233f9c @tag="650", @subfields=[#<MARC::Subfield:0x1233de4 @value="Indian women shamans", @code="a">, #<MARC::Subfield:0x1233da8 @value="Biography.", @code="v">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233d44 @tag="650", @subfields=[#<MARC::Subfield:0x1233b8c @value="Mohegan Indians", @code="a">, #<MARC::Subfield:0x1233b50 @value="Medicine.", @code="x">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233ad8 @tag="650", @subfields=[#<MARC::Subfield:0x1233920 @value="Delaware Indians", @code="a">, #<MARC::Subfield:0x12338e4 @value="Medicine.", @code="x">], @indicator2="0", @indicator1=" ">]> | ||
+ | irb(main):015:0> writer = MARC::Writer.new('/tmp/file') | ||
+ | => #<MARC::Writer:0x122fb68 @fh=#<File:/tmp/file>> | ||
+ | irb(main):016:0> writer.write(record) | ||
+ | => 990 | ||
+ | irb(main):018:0> writer.close | ||
+ | => nil | ||
+ | </pre> |
Revision as of 02:59, 21 March 2009
Rdocs are here: http://marc.rubyforge.org
It can be useful to play around with a library in irb before you try to write a real app with it. Here are some irb examples. If you need a file of MARC records to work with, you can download one from here: http://www.archive.org/search.php?query=mediatype%3A%22data%22%20MARC.
Recipe: read in a large file of marc records, pull out one of them and write it to a separate file
paz:blacklight eos8d$ irb irb(main):001:0> require 'rubygems' => true irb(main):002:0> require 'marc' => true irb(main):012:0> reader = MARC::Reader.new('/usr/local/projects/bl-demo/data/lc_records.utf8.mrc') => #<MARC::Reader:0x123dec0 @handle=#<File:/usr/local/projects/bl-demo/data/lc_records.utf8.mrc>> irb(main):014:0> record = reader.first => #<MARC::Record:0x1236c10 @leader="00990cam a22002894a 4500", @fields=[#<MARC::ControlField:0x1236594 @tag="001", @value=" 00008195 ">, #<MARC::ControlField:0x12364cc @tag="003", @value="DLC">, #<MARC::ControlField:0x1236404 @tag="005", @value="20090126093013.0">, #<MARC::ControlField:0x123633c @tag="008", @value="000128s2000 azuab b s000 0beng ">, #<MARC::DataField:0x1236274 @tag="010", @subfields=[#<MARC::Subfield:0x12360bc @value=" 00008195 ", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1236058 @tag="020", @subfields=[#<MARC::Subfield:0x1235eb4 @value="0816520682 (cloth : acid-free paper)", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1235e50 @tag="020", @subfields=[#<MARC::Subfield:0x1235cac @value="0816520690 (pbk. : acid-free paper)", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1235c48 @tag="040", @subfields=[#<MARC::Subfield:0x1235a90 @value="DLC", @code="a">, #<MARC::Subfield:0x1235a54 @value="DLC", @code="c">, #<MARC::Subfield:0x1235a18 @value="DLC", @code="d">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12359b4 @tag="042", @subfields=[#<MARC::Subfield:0x1235824 @value="pcc", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12357c0 @tag="043", @subfields=[#<MARC::Subfield:0x123561c @value="n-us-ct", @code="a">, #<MARC::Subfield:0x12355e0 @value="n-us---", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x123557c @tag="050", @subfields=[#<MARC::Subfield:0x12353d8 @value="E99.M83", @code="a">, #<MARC::Subfield:0x123539c @value="T364 2000", @code="b">], @indicator2="0", @indicator1="0">, #<MARC::DataField:0x1235338 @tag="082", @subfields=[#<MARC::Subfield:0x123516c @value="974.7/004973", @code="a">, #<MARC::Subfield:0x1235130 @value="B", @code="a">, #<MARC::Subfield:0x12350f4 @value="21", @code="2">], @indicator2="0", @indicator1="0">, #<MARC::DataField:0x1235090 @tag="100", @subfields=[#<MARC::Subfield:0x1234ed8 @value="Zobel, Melissa Tantaquidgeon,", @code="a">, #<MARC::Subfield:0x1234e9c @value="1960-", @code="d">], @indicator2=" ", @indicator1="1">, #<MARC::DataField:0x1234e38 @tag="245", @subfields=[#<MARC::Subfield:0x1234c6c @value="Medicine trail :", @code="a">, #<MARC::Subfield:0x1234c1c @value="the life and lessons of Gladys Tantaquidgeon /", @code="b">, #<MARC::Subfield:0x1234bcc @value="Melissa Jayne Fawcett.", @code="c">], @indicator2="0", @indicator1="1">, #<MARC::DataField:0x1234b68 @tag="260", @subfields=[#<MARC::Subfield:0x12349b0 @value="Tucson :", @code="a">, #<MARC::Subfield:0x1234960 @value="University of Arizona Press,", @code="b">, #<MARC::Subfield:0x1234924 @value="c2000.", @code="c">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12348c0 @tag="300", @subfields=[#<MARC::Subfield:0x12346f4 @value="xvi, 177 p. :", @code="a">, #<MARC::Subfield:0x12346a4 @value="ill., map ;", @code="b">, #<MARC::Subfield:0x1234668 @value="21 cm.", @code="c">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x1234604 @tag="504", @subfields=[#<MARC::Subfield:0x1234460 @value="Includes bibliographical references (p. 177).", @code="a">], @indicator2=" ", @indicator1=" ">, #<MARC::DataField:0x12343fc @tag="600", @subfields=[#<MARC::Subfield:0x1234258 @value="Tantaquidgeon, Gladys.", @code="a">], @indicator2="0", @indicator1="1">, #<MARC::DataField:0x12341f4 @tag="650", @subfields=[#<MARC::Subfield:0x123403c @value="Mohegan Indians", @code="a">, #<MARC::Subfield:0x1234000 @value="Biography.", @code="v">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233f9c @tag="650", @subfields=[#<MARC::Subfield:0x1233de4 @value="Indian women shamans", @code="a">, #<MARC::Subfield:0x1233da8 @value="Biography.", @code="v">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233d44 @tag="650", @subfields=[#<MARC::Subfield:0x1233b8c @value="Mohegan Indians", @code="a">, #<MARC::Subfield:0x1233b50 @value="Medicine.", @code="x">], @indicator2="0", @indicator1=" ">, #<MARC::DataField:0x1233ad8 @tag="650", @subfields=[#<MARC::Subfield:0x1233920 @value="Delaware Indians", @code="a">, #<MARC::Subfield:0x12338e4 @value="Medicine.", @code="x">], @indicator2="0", @indicator1=" ">]> irb(main):015:0> writer = MARC::Writer.new('/tmp/file') => #<MARC::Writer:0x122fb68 @fh=#<File:/tmp/file>> irb(main):016:0> writer.write(record) => 990 irb(main):018:0> writer.close => nil