Instantly share code, notes, and snippets.

Embed
What would you like to do?
2014/11/12のブログ記事のサンプルコード
class Post
def initialize(title, body)
@title, @body = title, body
end
def render
<<-EOS
<article>
<h1>#{@title}</h1>
<p>#{@body}</p>
</article>
EOS
end
end
post = Post.new('タイトル', '記事の内容')
puts post.render
# =>
# <article>
# <h1>タイトル</h1>
# 記事の内容
# </article>
class Post
def initialize(title, body)
@title, @body = title, body
end
def render
<<-EOS
<article>
<h1>#{@title}</h1>
<p>#{@body}</p>
</article>
EOS
end
def self.find(filename)
path = File.join(File.dirname(__FILE__), filename)
title, body = File.read(path).split("\n", 2).map(&:strip)
Post.new(title, body)
end
end
post = Post.find('foo.txt')
puts post.render
class Post
def self.render(title, body)
<<-EOS
<article>
<h1>#{title}</h1>
<p>#{body}</p>
</article>
EOS
end
end
puts Post.render('タイトル', '記事の内容')
# =>
# <article>
# <h1>タイトル</h1>
# 記事の内容
# </article>
class Post
def self.render(title, body)
<<-EOS
<article>
<h1>#{title}</h1>
<p>#{body}</p>
</article>
EOS
end
def self.find(filename)
path = File.join(File.dirname(__FILE__), filename)
title, body = File.read(path).split("\n", 2).map(&:strip)
{ title: titile, body: body }
end
end
post_hash = Post.find('foo.txt')
Post.render(post_hash[:title], post_hash[:body])
class Post
def self.render(title, keyword, description, category, body)
<<-EOS
<article>
<h1>#{title}</h1>
<p>#{keyword} #{description} #{category}</p>
<p>#{body}</p>
</article>
EOS
end
def self.find(filename)
path = File.join(File.dirname(__FILE__), filename)
title, keyword, description, category, body = File.read(path).split("\n", 5).map(&:strip)
{ title: titile, keyword: keyword, description: description, category: category, body: body }
end
end
post_hash = Post.find('foo.txt')
Post.render(post_hash[:title], post_hash[:keyword], post_hash[:description], post_hash[:category], post_hash[:body])
class Post
def initialize(title, keyword, description, category, body)
@title = title
@keyword = keyword
@description = description
@category = category
@body = body
end
def render
<<-EOS
<article>
<h1>#{@title}</h1>
<p>#{@keyword} #{@description} #{@category}</p>
<p>#{@body}</p>
</article>
EOS
end
def self.find(filename)
path = File.join(File.dirname(__FILE__), filename)
title, keyword, description, category, body = File.read(path).split("\n", 5).map(&:strip)
Post.new(title, keyword, description, category, body)
end
end
post = Post.find('foo.txt')
post.render
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment