5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Faker使ってみたら面白かった話

Last updated at Posted at 2019-01-31

はじめに

今回はFakerというgemを使ってみました。
Fakerはダミーデータを生成したいときに使われると思いますが、
生成できるダミーデータがユニークだったので記事にしてみました。

ただし、今回はFakerの使い方については触れず、
個人的に面白いと思ったところを書いていきます。

手元でも試してみたい方は、Gemfileに下記を追加してbundleするか、
直接gem install 'faker'でインストールしてください。

Gemfile
gem 'faker'

触ってみる

Fakerを使ってダミーデータを生成する場合、
例えばNameであればこのような形になると思います。

irb
Faker::Name.name
=> "Altagracia Feeney"

「他にどんなダミーデータを作れるんだろう」と思い、
Fakerが持つ定数を取得してみました。

irb
Faker.constants
=> [:LoremPixel, :Stripe, :Lovecraft, :String, :Markdown, :Superhero, :SwordArtOnline, :Name,
 :Team, :TheFreshPrinceOfBelAir, :Matz, :Measurement, :TheThickOfIt, :TheITCrowd, :TwinPeaks,
 :MichaelScott, :Military, :Crypto, :Currency, :MostInterestingManInTheWorld, :Movie, :Music,
 :Demographic, :Myst, :UniqueGenerator, :Dessert, :Device, :Nation, :Dog, :NatoPhoneticAlphabet,
 :NewGirl, :Dota, :Base, :DrWho, :File, :DragonBall, :DumbAndDumber, :Dune, :Educator, :Omniauth,
 :UmphreysMcgee, :University, :ElderScrolls, :Job, :VForVendetta, :Vehicle, :Address,
 :ElectricalComponents, :Ancient, :Color, :Esport, :Ethereum, :Fallout, :FamilyGuy,
 :FamousLastWords, :Internet, :Fillmurray, :Finance, :Twitter, :VentureBros, :App, :Verb,
 :Appliance, :Food, :AquaTeenHungerForce, :Witcher, :Artist, :Avatar, :WorldCup, :Football,
 :Lorem, :WorldOfWarcraft, :BackToTheFuture, :Bank, :FunnyName, :Friends, :Yoda, :Zelda,
 :GameOfThrones, :PhoneNumber, :Gender, :GreekPhilosophers, :Hacker, :Beer, :HarryPotter,
 :HeyArnold, :Hipster, :Bitcoin, :VERSION, :Number, :Source, :BojackHorseman,
 :HitchhikersGuideToTheGalaxy, :Book, :OnePiece, :Hobbit, :HowIMetYourMother, :BossaNova,
 :IDNumber, :Overwatch, :BreakingBad, :Placeholdit, :Business, :ParksAndRec, :Pokemon,
 :PrincessBride, :Cannabis, :ProgrammingLanguage, :RickAndMorty, :Robin, :RockBand, :RuPaul,
 :Char, :Science, :Cat, :Compass, :Seinfeld, :ChuckNorris, :Shakespeare, :Code, :SiliconValley,
 :Simpsons, :SingularSiegler, :SlackEmoji, :Date, :Invoice, :Space, :Coffee, :Time, :Kpop, :Types,
 :Commerce, :Boolean, :LeagueOfLegends, :StarTrek, :Config, :StarWars, :Lebowski, :LordOfTheRings,
 :Community, :Company, :LoremFlickr, :Stargate, :StrangerThings]

よく見ると「Pokemon」や「OnePiece」といった見覚えのある文字が...
今回は個人的に好きな「OnePiece」を例に見てみます。
まずは、Faker::OnePieceが扱えるメソッドを取得してみます。

irb
Faker::OnePiece.methods(false)
=> [:quote, :location, :sea, :island, :akuma_no_mi, :character]

何やら悪魔の実やキャラクターなどのダミーデータを生成できそうですね。
まずは、悪魔の実を試してみましょう。

irb
Faker::OnePiece.akuma_no_mi
=> "Ope Ope no Mi"

おーダミーデータとして、オペオペの実が生成されました。
ついでにキャラクターの方も見てみましょう。

irb
Faker::OnePiece.character
=> "Aokiji"

なんと、青キジが生成されましたね!

まとめ

今回はFakerを使って「OnePiece」のダミーデータを生成してみました。
これらを使って何か面白そうなことができれば、また記事にしたいと思います。
他にもいろいろ面白そうなのがあったので、
興味がある方は試してみてください!

5
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?