Monday 6 June 2016

Ruby Conditional statements and Iterators

Hi Readers,

In this we are going to learn conditions and Iterators in Ruby. Please follow the below concepts.

if, unless, and the ? Operator

message = if count > 10 
            "Try again" 
          elsif tries == 3 
            "You lose" 
          else 
            "Enter command" 
          end 
 
raise "Unauthorized" if !current_user.admin? 
raise "Unauthorized" unless current_user.admin? 
 
status = input > 10 ? "Number too big" : "ok"  

Iterators: while, until, for, break, and next

while count < 100 
  puts count 
  count += 1 
end 
 
payment.make_request while (payment.failure? and payment.tries < 3) 
 
for user in @users 
  next if user.admin? 
  if user.paid? 
    break 
  end 
end 
 
until count > 5 
  puts count 
  count += 1 
end   


Practice with different conditions,
Please share this to help others.

 


No comments:

Post a Comment

My Experience in RUBYCONF INDIA 2016

Hi Readers, Rubyconf India is a global event complementing other RubyConf events across the world. I attended this event held on 19-20 ma...