#If, ElsIf, Else
if @names.nil? #Check if object is null/not initialized
puts "@names not yet initialized"
elsif @names.respond_to?("join") #Check if object is a list
puts "@names seems to have more than 1 element "
else
puts "only element is #{@name}"
end
#each/all elements in list
names=["ABC","DEF","GHI"]
names.each do |name|
puts "Hello #{name}"
end
Hello ABC
Hello DEF
Hello GHI
=> ["ABC", "DEF", "GHI"]
puts "All are #{names.join(", ")}."
All are ABC, DEF, GHI.
=> nil
#save scripts to file and run as follows. e.g can be downloaded from here.
ruby ruby_script_example.rb
ruby ruby_script_example.rb Rahul Rohit
Note: __FILE__ can be used in script to decide about execution of statements in file, just like main method in other language.