Difference between revisions of "Ruby-marc"

From Code4Lib
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Rdocs are here: [http://marc.rubyforge.org http://marc.rubyforge.org]
 
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 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].
+
These are some recipes for useful things you might want to do with ruby-marc. 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 ====
 
==== 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
+
<pre>
irb(main):001:0> require 'rubygems'
+
require 'rubygems'
=> true
+
require 'marc'
irb(main):002:0> require 'marc'
+
 
=> true
+
reader = MARC::Reader.new('/usr/local/projects/bl-demo/data/lc_records.utf8.mrc')
irb(main):012:0> reader = MARC::Reader.new('/usr/local/projects/bl-demo/data/lc_records.utf8.mrc')
+
record = reader.first
=> #<MARC::Reader:0x123dec0 @handle=#<File:/usr/local/projects/bl-demo/data/lc_records.utf8.mrc>>
+
writer = MARC::Writer.new('/tmp/file2')
irb(main):014:0> record = reader.first
+
writer.write(record)
=> #<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=" ">]>
+
writer.close
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
+
irb(main):003:0> exit
+
paz:blacklight eos8d$ cat /tmp/file
+
00990cam a22002894a 4500001001300000003000400013005001700017008004100034010001700075020004100092020004000133040001800173042000800191043002100199050002300220082002400243100004100267245009300308260005100401300003900452504005000491600002700541650003200568650003700600650003100637650003200668  00008195 DLC20090126093013.0000128s2000    azuab    b  s000 0beng    a  00008195  a0816520682 (cloth : acid-free paper)  a0816520690 (pbk. : acid-free paper)  aDLCcDLCdDLC  apcc  an-us-ctan-us---00aE99.M83bT364 200000a974.7/004973aB2211 aZobel, Melissa Tantaquidgeon,d1960-10aMedicine trail :bthe life and lessons of Gladys Tantaquidgeon /cMelissa Jayne Fawcett.  aTucson :bUniversity of Arizona Press,cc2000.  axvi, 177 p. :bill., map ;c21 cm.  aIncludes bibliographical references (p. 177).10aTantaquidgeon, Gladys. 0aMohegan IndiansvBiography. 0aIndian women shamansvBiography. 0aMohegan IndiansxMedicine. 0aDelaware IndiansxMedicine.paz:blacklight
+
 
</pre>
 
</pre>

Latest revision as of 03:05, 21 March 2009

Rdocs are here: http://marc.rubyforge.org

These are some recipes for useful things you might want to do with ruby-marc. 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

require 'rubygems'
require 'marc'

reader = MARC::Reader.new('/usr/local/projects/bl-demo/data/lc_records.utf8.mrc')
record = reader.first
writer = MARC::Writer.new('/tmp/file2')
writer.write(record)
writer.close