Ruby From Beginning: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Thomas (Diskussion | Beiträge) |
Thomas (Diskussion | Beiträge) |
||
| Zeile 21: | Zeile 21: | ||
VarFile.puts "das zweite" | VarFile.puts "das zweite" | ||
VarFile.close | 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 | ||
Version vom 3. November 2017, 11:35 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