LoginSignup
0
0

More than 3 years have passed since last update.

to_○ 

Posted at

to_○

x = 50
y = "3"

p x + y.to_i #to int
p x + y.to_f #to floo


scores = {keiichi:400, tomomi:2000}

p scores.to_a #to arry 
p scores.to_h #to hassh 

%記法


puts ("red", "blue")

puts %(hello) #でOK 間に””を使用できる

p ["red", "blue"]

p %W(red, blue) #でOK

if


score = gets.to_i


if score > 80 then
        puts "great!"
else if score > 60 then
    puts "good"

else
    puts "so so..."

end

case

#case

signal = gets.chomp #最後の改行コードを取り除くchomp

case signal
when "red" 
    puts "stop!"
when "green" 
    puts "go!"
when "yello" 
    puts "caution!"

else
    puts "wrong signal"

end

While

#while

i = 0

while i < 10 do

    puts "#{i}: hello" # #{i}は回数を見やすくする為に使用

    i += 1 #i = i + 1を短縮して書ける

end

Times

#times

10.times do |i| #i += 1と同じ効果
    puts "hello"
end

for


for i in 15..20 do
    p i
end

for color in ["red", "blue"] do
    p color
end

for name, score in {taguchi:200, fkoji:400} do
    puts "#{name}: #{score}"
end

for color in %W(red, bule) do
    p color
end

each


(15..20).each do |i|
    p i
end

["red", "blue"].each do |color|
    p color
end

{taguchi:200, fkoji:400}.each do |name, score|
    puts "#{name}: #{score}"
end

%W(red, bule).each do |color|
    p color
end
0
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
0
0