Ruby From Beginning

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen

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