Commit 0c68b17c by Trịnh Hoàng Phúc

Ruby learning exercises

parent d1fb2fbc
Pipeline #487 failed with stages
in 0 seconds
cars = 100
puts "Have #{cars}"
\ No newline at end of file
formater = "%{fisrt} %{second} %{third} %{fourth}"
puts formater % {fisrt: 1, second: 2, third: 3, fourth: 4}
puts formater % {fisrt: 'one', second: 'two', third: 'three', fourth: 'four'}
puts formater % {fisrt: formater, second: formater, third: formater, fourth: formater}
\ No newline at end of file
print "How old are you?"
age = gets.chomp
print "How tall are you?"
height = gets.chomp
print "How much do you weigh?"
weight = gets.chomp
puts "#{age} #{height} #{weight}"
\ No newline at end of file
first, second, third = ARGV
puts "Your first variable is: #{first}"
puts "Your second variable is: #{second}"
puts "Your third variable is: #{third}"
user_name = ARGV.first
prompt = '> '
puts "Hi #{user_name}"
puts "I would like to ask you a few questions"
puts "Do you like me #{user_name}"
puts prompt
likes = $stdin.gets.chomp
puts "Where do you live #{user_name}?"
puts prompt
lives = $stdin.gets.chomp
puts "What kind of computer do you have? ", prompt
computer = $stdin.gets.chomp
puts """
Alright, so you said #{likes} about liking me.
You live in #{lives}.
And you have a #{computer}
"""
filename = ARGV.first
txt = open(filename)
puts "Here's your file #{filename}"
print txt.read
print "Type the filename again: "
file_again = $stdin.gets.chomp
txt_again = open(file_again)
print txt_again.read
\ No newline at end of file
Read me please!
\ No newline at end of file
filename = ARGV.first
puts "We are going to erase #{filename}"
$stdin.gets
puts "Opening the file..."
target = open(filename, 'w')
puts "Truncating the file."
target.truncate(0)
puts "Now I'm going to ask you for three lines"
print "line 1: "
line1 = $stdin.gets.chomp
print "line 2: "
line2 = $stdin.gets.chomp
print "line 3: "
line3 = $stdin.gets.chomp
puts "Write these to the file"
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
puts "Close it"
target.close
\ No newline at end of file
line 1
line 2
line 3
\ No newline at end of file
from_file, to_file = ARGV
puts "Copying from #{from_file} to #{to_file}"
in_file = open(from_file)
indata = in_file.read
puts "The input file is #{indata.length} bytes long"
puts "Does the output file exist? #{File.exist?(to_file)}"
puts "Ready, hit RETURN to continue, CTRL-C to abort."
$stdin.gets
out_file = open(to_file, 'w')
out_file.write(indata)
puts "Alright, all done"
out_file.close
in_file.close
\ No newline at end of file
Ex8 test
def print_two(*args)
arg1, arg2 = args
puts "arg1: #{arg1}, arg2: #{arg2}"
end
def print_two_again(arg1, arg2)
puts "arg1: #{arg1}, arg2: #{arg2}"
end
print_two(1,2)
print_two_again(1,2)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment