class Score
def initialize(subject, score)
@subject = subject
@score = score
end
def get_info
"#{@subject}/#{@score}"
end
end
class User
def initialize(name, score)
@name = name
@score = score
end
def get_info
"Name: #{@name}, Score: #{@score.get_info}"
end
end
user1 = User.new("Taro", Score.new("Math", 70))
user2 = User.new("Jiro", Score.new("English", 90))
puts user1.get_info
puts user2.get_info
タグ: programming
ruby rssserver
require ‘cgi’
require ‘rss’
require ‘net/http’
def parse(page_source)
dates = page_source.scan(%r!(\d+)年 ?(\d+)月 ?(\d+)日
!).map { |year, month, day| [year.to_i, month.to_i, day.to_i] }
url_titles = page_source.scan(%r!^(.+?)
!).map { |url, title| [CGI.unescapeHTML(url), CGI.unescapeHTML(title)] }
url_titles.zip(dates).map { |(url, title), date| [url, title, Time.local(*date)] }
end
def format_text(title, url, url_title_time_ary)
text = “Title: #{title}\nURL: #{url}\n\n”
url_title_time_ary.each do |aurl, atitle, atime|
text << “* (#{atime})#{atitle}\n”
text << ” #{aurl}\n”
end
text
end
def format_rss(title, url, url_title_time_ary)
RSS::Maker.make(“2.0”) do |maker|
maker.channel.updated = Time.now.to_s
maker.channel.link = url
maker.channel.title = title
maker.channel.description = title
url_title_time_ary.each do |aurl, atitle, atime|
maker.items.new_item do |item|
item.link = aurl
item.title = atitle
item.updated = atime
item.description = atitle
end
end
end
end
uri = URI(“http://crawler.sbcr.jp/samplepage.html”)
response = Net::HTTP.get_response(uri)
parsed = parse(response.body.force_encoding(“UTF-8”))
formatter = case ARGV.first
when “rss-output”
:format_rss
else
:format_text
end
puts send(formatter, “WWW.SBCR.JP トピックス”, “http://crawler.sbcr.jp/samplepage.html”, parsed)
Ruby RSS
require 'rss'
require 'open-uri'
# RSSフィードのURL
rss_url = 'http://2ch-ranking.net/rss/livemarket1.rdf'
# RSSフィードを取得してパース
rss_content = URI.open(rss_url).read
rss = RSS::Parser.parse(rss_content)
# フィードのタイトルと記事を出力
puts "フィードのタイトル: #{rss.channel.title}"
puts "-----"
rss.items.each do |item|
puts "タイトル: #{item.title}"
puts "リンク: #{item.link}"
puts "概要: #{item.description}"
puts "-----"
end
Ruby入門 クラス編
class Score
def initialize(subject, score)
@subject = subject
@score = score
end
end
class User
def initialize(name, score)
@name = name
@score = score
end
end
user1 = User.new("Taro", Score.new("Math", 70))
user2 = User.new("Jiro", Score.new("English", 90))
ruby クラスメソッド
class User
@@count = 0
def initialize(name, score)
@name = name
@score = score
@@count += 1
end
def get_info
"Name: #{@name}, Score: #{@score}"
end
def self.get_info
"Count: #{@@count}"
end
end
# count = 0
user1 = User.new("Taro", 70)
# count += 1
user2 = User.new("Jiro", 90)
# count += 1
# puts count
puts User.get_info
ruby attr_accessorruby
class User
# attr_accessor :name
# attr_writer :name
attr_reader :name
def initialize(name, score)
@name = name
@score = score
end
# # getter
# def name
# @name
# end
# # setter
# def name=(new_name)
# @name = new_name
# end
def get_info
"Name: #{@name}, Score: #{@score}"
end
end
user1 = User.new("Taro", 70)
user2 = User.new("Jiro", 90)
user1.name = "TARO"
puts user1.name
ruby インスタンス変数
class User
def initialize(name, score)
@name = name
@score = score
end
def name
@name
end
def name=(new_name)
@name = new_name
end
def get_info
"Name: #{@name}, Score: #{@score}"
end
end
user1 = User.new("Taro", 70)
user2 = User.new("Jiro", 90)
user1.name = "TARO"
puts user1.name
ruby イニシャライザ
class User
def initialize(name, score)
@name = name
@score = score
end
def get_info
"Name: #{@name}, Score: #{@score}"
end
end
user1 = User.new("Taro", 70)
user2 = User.new("Jiro", 90)
puts user1.get_info
puts user2.get_info
ruby userクラス
class User
def get_info
"user name and score here"
end
end
user1 = User.new
user2 = User.new
puts user1.get_info
puts user2.get_info
TailwindCSS Todoリスト
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Todo List with Tailwind CSS</title>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 h-screen flex justify-center items-center">
<div class="max-w-md w-full">
<h1 class="text-center text-2xl font-bold mb-4">Todo List</h1>
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<div class="mb-4">
<input type="text" placeholder="Add new task..." class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" id="taskInput">
</div>
<div class="flex items-center justify-between">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" id="addTaskBtn">
Add Task
</button>
<div>
<button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="all">
All
</button>
<button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="active">
Active
</button>
<button class="bg-gray-300 hover:bg-gray-400 text-gray-800 font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline filter-btn" data-filter="completed">
Completed
</button>
</div>
</div>
</div>
<ul id="taskList" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<!-- Tasks will be dynamically added here -->
</ul>
<div class="flex justify-between">
<p id="taskCounter" class="text-gray-600"></p>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" id="clearCompletedBtn">
Clear Completed
</button>
</div>
</div>
<script>
let tasks = [];
function renderTasks(filter) {
const taskList = document.getElementById("taskList");
taskList.innerHTML = "";
tasks.forEach((task, index) => {
if (filter === "all" || (filter === "active" && !task.completed) || (filter === "completed" && task.completed)) {
const listItem = document.createElement("li");
listItem.className = "flex justify-between items-center border-b py-2";
listItem.innerHTML = `
<span class="text-gray-700 ${task.completed ? 'line-through' : ''}">${task.text}</span>
<div>
<button class="text-green-500 hover:text-green-700 focus:outline-none" onclick="toggleTaskStatus(${index})">
${task.completed ? `
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M0 11l2-2 5 5L18 3l2 2L7 18z" clip-rule="evenodd" />
</svg>
` : `
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M19 9l-9 9-5-5"></path>
</svg>
`}
</button>
<button class="text-blue-500 hover:text-blue-700 focus:outline-none" onclick="editTask(${index})">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M3 17a2 2 0 01-2-2V6a2 2 0 012-2h5a2 2 0 011 3.732V15h5a2 2 0 012 2v2a2 2 0 01-2 2H3zm12-7V5a1 1 0 00-1-1H7a1 1 0 00-1 1v5h9zm-4 3h2v2h-2v-2z" clip-rule="evenodd"></path>
</svg>
</button>
<button class="text-red-500 hover:text-red-700 focus:outline-none" onclick="deleteTask(${index})">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" viewBox="0 0 20 20" fill="currentColor">
<path fill-rule="evenodd" d="M5.293 6.293a1 1 0 011.414 1.414L11 11.414l4.293-4.293a1 1 0 111.414 1.414L12.414 12l4.293 4.293a1 1 0 01-1.414 1.414L11 13.414l-4.293 4.293a1 1 0 01-1.414-1.414L9.586 12 5.293 7.707a1 1 0 010-1.414z" clip-rule="evenodd" />
</svg>
</button>
</div>
`;
taskList.appendChild(listItem);
}
});
updateTaskCounter();
}
function addTask(text) {
tasks.push({ text: text, completed: false });
renderTasks();
}
function deleteTask(index) {
tasks.splice(index, 1);
renderTasks();
}
function editTask(index) {
const newText = prompt("Edit Task", tasks[index].text);
if (newText !== null) {
tasks[index].text = newText.trim();
renderTasks();
}
}
function toggleTaskStatus(index) {
tasks[index].completed = !tasks[index].completed;
renderTasks();
}
function clearCompletedTasks() {
tasks = tasks.filter(task => !task.completed);
renderTasks();
}
function updateTaskCounter() {
const taskCounter = document.getElementById("taskCounter");
const activeTasks = tasks.filter(task => !task.completed).length;
const taskWord = activeTasks === 1 ? "task" : "tasks";
taskCounter.textContent = `${activeTasks} ${taskWord} left`;
}
document.getElementById("addTaskBtn").addEventListener("click", function() {
const taskInput = document.getElementById("taskInput");
const taskText = taskInput.value.trim();
if (taskText !== "") {
addTask(taskText);
taskInput.value = "";
}
});
document.getElementById("clearCompletedBtn").addEventListener("click", function() {
clearCompletedTasks();
});
document.querySelectorAll(".filter-btn").forEach(btn => {
btn.addEventListener("click", function() {
const filter = this.getAttribute("data-filter");
renderTasks(filter);
});
});
// Initial render
renderTasks();
</script>
</body>
</html>