1
0

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.

String#*の引数を「0」にすると「""」になる( 例:"foobar" * 0 # => "" )ので遊んでみた。

Last updated at Posted at 2012-03-16
# coding: UTF-8

# 1.nth毎の項目を「""」に変換する
# 例1:DATAから「"----------"」の行(3行毎に存在)を削除する
# 結果:
# => あいうえお
# => かきくけこ
# => さしすせそ
# => たちつてと
# => なにぬねの
# => はひふへほ
# => まみむめも
# => や ゆ よ
# => らりるれろ
# => わ   を
# => ん    

nth = 3
puts data = DATA.lines.map.with_index(1){ |e,i| e * ( i%nth <=> 0 ) }.reject(&:empty?)

# 2.nth毎以外の項目を「""」に変換する
# 例2:例1の結果からひらがなポスターを作る
# 結果:
# => んわらやまはなたさかあ
# =>   り みひにちしきい
# =>   るゆむふぬつすくう
# =>   れ めへねてせけえ
# =>  をろよもほのとそこお

data = data.map(&:chomp).join.chars

nth = 5
puts 1.upto(nth).map { |n|
  data.map.with_index(n){ |e,i| e * ( ( 0 <=> i%nth ) + 1 )}.reverse.join
}.reverse

__END__
あいうえお
かきくけこ
----------
さしすせそ
たちつてと
----------
なにぬねの
はひふへほ
----------
まみむめも
や ゆ よ
----------
らりるれろ
わ   を
----------
ん    

1
0
0

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?