day 1
- Print the string “Hello, world.” puts "Hello, world."
- For the string “Hello, Ruby,” find the index of the word “Ruby.” "Hello, Ruby.".index("Ruby")
- Print your name ten times. 10.times{puts "Li Chao"}
- Print the string “This is sentence number 1,” where the number 1 changes from 1 to 10. (0..10).each do |i| puts "This is sentence number #{i}" end or: for i in 0..10 puts "This is sentence number #{i}" end
- Run a Ruby program from a file. save ex.rb with: for i in 0..10 puts "This is sentence number #{i}" end then run: jruby ex.rb
- Bonus problem: If you’re feeling the need for a little more, write a program that picks a random number. Let a player guess the number, telling the player whether the guess is too low or too high.(Hint: rand(10) will generate a random number from 0 to 9, and gets will read a string from the keyboard that you can translate to an integer.) target = rand(10) puts "Please input a number between 0~10:" i = gets.chomp.to_i while i != target if i<target puts "lower" else puts "higher" end i = gets.chomp.to_i end puts "You got it: #{i}"