Ruby From Beginning: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Thomas (Diskussion | Beiträge) |
Thomas (Diskussion | Beiträge) |
||
| Zeile 32: | Zeile 32: | ||
puts "I can't guess the number" | puts "I can't guess the number" | ||
end | end | ||
| + | =CASE Statement= | ||
| + | #!/usr/bin/ruby | ||
| + | print "Enter your grade: " | ||
| + | grade = gets.chomp | ||
| + | case grade | ||
| + | when "A" | ||
| + | puts 'You pretty smart!' | ||
| + | when "B" | ||
| + | puts 'You smart!' | ||
| + | when "C", "D" | ||
| + | puts 'You pretty dumb!!' | ||
| + | else | ||
| + | puts "You can't even use a computer!" | ||
| + | end | ||
Version vom 3. November 2017, 13:14 Uhr
Hello World
#!/usr/bin/ruby puts "Hello World!"
What is your name?
#!/usr/bin/ruby
print "What is your name? "
name = gets
puts "Your name is #{name}"
Read a file
#!/usr/bin/ruby
puts "What file do you want to read? "
Filename = gets
Filename.delete!("\n")
TheFile = File.read( Filename )
puts TheFile.to_s
Write in a file
#!/usr/bin/ruby
puts "write"
VarFile = File.new("dat.txt", "w+")
VarFile.puts "das erste "
VarFile.puts "das zweite"
VarFile.close
IF Statement
#!/usr/bin/ruby
puts "Number?: "
x = gets
if x.to_i > 2
puts "x is greater than 2"
elsif x.to_i <= 2 and x.to_i != 0
puts "x is 1"
else
puts "I can't guess the number"
end
CASE Statement
#!/usr/bin/ruby print "Enter your grade: " grade = gets.chomp case grade when "A" puts 'You pretty smart!' when "B" puts 'You smart!' when "C", "D" puts 'You pretty dumb!!' else puts "You can't even use a computer!" end