def dividable?(a, b)
# if b != 0
# if a % b == 0
# true
# else
# false
# end
# else
# false
# end
if b != 0 && a % b == 0
true
else
false
end
end
puts dividable?(10, 2) # true
puts dividable?(10, 3) # false
puts dividable?(10, 0)
カテゴリー: programming
ruby puts 真偽値を返すメソッド
def dividable?(a, b)
if a % b == 0
true
else
false
end
end
puts dividable?(10,2) # true
puts dividable?(10,3) #false
ruby メソッド
def show_ad
puts "---------"
puts "SALE! 50% OFF!"
puts "---------"
end
def show_content
puts "BREAKING NEWS!"
puts "Two baby pandas born at our Zoo!"
end
show_ad
show_content
show_ad
ruby caseで条件分岐
print "Score? "
score = gets.to_i
case score
when 100
puts "Super"
when 90..89
puts "A"
when 70...89
puts "B"
else
puts"C"
end
ruby メモアプリ
require 'date'
class Memo
attr_accessor :title, :content, :category, :created_at
def initialize(title, content, category)
@title = title
@content = content
@category = category
@created_at = DateTime.now
end
def to_s
"#{@title}: #{@content} (Category: #{@category}) [Created at: #{@created_at}]"
end
end
class MemoApp
def initialize
@memos = []
load_memos
end
def run
loop do
show_menu
choice = gets.chomp.to_i
case choice
when 1
create_memo
when 2
display_memos
when 3
search_memos
when 4
break
else
puts "Invalid choice. Please try again."
end
end
end
private
def show_menu
puts "\nMemo App Menu"
puts "1. Create Memo"
puts "2. Display Memos"
puts "3. Search Memos"
puts "4. Exit"
print "Enter your choice: "
end
def create_memo
print "Enter memo title: "
title = gets.chomp
print "Enter memo content: "
content = gets.chomp
print "Enter memo category: "
category = gets.chomp
memo = Memo.new(title, content, category)
@memos << memo
save_memos
puts "Memo created successfully!"
end
def display_memos
if @memos.empty?
puts "No memos found."
else
puts "\nMemos:"
@memos.each_with_index do |memo, index|
puts "#{index + 1}. #{memo}"
end
end
end
def search_memos
print "Enter search term: "
term = gets.chomp.downcase
results = @memos.select { |memo| memo.title.downcase.include?(term) || memo.content.downcase.include?(term) || memo.category.downcase.include?(term) }
if results.empty?
puts "No memos found matching the search term."
else
puts "\nSearch Results:"
results.each { |memo| puts memo }
end
end
def load_memos
if File.exist?("memos.txt")
File.open("memos.txt", "r") do |file|
file.each_line do |line|
title, content, category, created_at = line.chomp.split(": ")
@memos << Memo.new(title, content, category)
end
end
end
end
def save_memos
File.open("memos.txt", "w") do |file|
@memos.each do |memo|
file.puts "#{memo.title}: #{memo.content}: #{memo.category}: #{memo.created_at}"
end
end
end
end
# メモアプリの実行
memo_app = MemoApp.new
memo_app.run
ruby 特定の文字にアクセス
print "Your name? "
name = gets
puts name[0]
puts name[1]
puts name[2]
name[0] = "*"
name[1] = "*"
name[2] = "*"
puts name
Ruby 文字列を操作
print "Your name? "
name = gets
puts "*" * 10
puts "Hello #{name}"
puts "Hello " + name
puts "*" * 10
ruby ユーザーからの入力
print "Your name?"
name = gets
puts name
Ruby 文字列
#puts 'Hello Taro'
#puts 'Hello Jiro'
#puts "Hello\nSaburo"
name = "Shiro"
puts "Hello #{name}"
Ruby 変数に値を代入
price = 150
puts price * 120
puts price * 130
puts price * 140
# price = 151
# price = price + 1
price += 1
#price -= 1
puts price * 120
puts price * 130
puts price * 140
