Wednesday, October 14, 2009

Ruby on Rails : For / While / Do While / Switch

MethodExample CodeOutput
For Loop
<% for i in 0..9 %>
Result: <%= i %>,
<% end %>
Result: 0,1,2,3,4,5,6,7,8,9
<% @people = Person.find(:all) %>
<% for i in 0...@person.length %>
Result: <%= @person[i].name %>,
<% end %>
Result: John,Peter,Sharon
<% @people = Person.find(:all) %>
<% for person in @people %>
Result: <%= @person.name %>,
<% end %>
Result: John,Peter,Sharon
While Loop
<% i = 0 %>
<% while( i < 10 ) %>
Result: <%= i %>,
<% i+= 1 %>
<% end %>
Result: 0,1,2,3,4,5,6,7,8,9
Do While Loop
<% i = 5 %>
<% begin %>
Result: <%= i %>,
<% i+= 1 %>
<% end while( i < 10 ) %>
Result: 5,6,7,8,9
Switch Statement
<% foo = 5 %>
<% case foo %>
<% when 1 %>
   Foo is equal to 1
<% when 2..9 %>
   Foo is between 2 and 9
<% when 10 %>
   Foo is equal to 10
<% end %>
Foo is between 2 and 9

10 comments:

  1. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Javascript Training in Chennai . or learn thru JavaScript Online Training India. Nowadays JavaScript has tons of job opportunities on various vertical industry

    ReplyDelete
  2. After reading this blog i very strong in this topics and this blog really helpful to all... explanation are very clear so very easy to understand... thanks a lot for sharing this blog.get more info..Ruby on Rails Online Training

    ReplyDelete
  3. This is a terrific article, and that I would really like additional info if you have got any. I’m fascinated with this subject and your post has been one among the simplest I actually have read.
    Data Science Tutorial
    Data Science training in anna nagar
    Data science training in jaya nagar
    Data science training in pune
    Data Science Training in Marathahalli
    Data science training in kalyan nagar

    ReplyDelete
  4. This is a terrific article, and that I would really like additional info if you have got any ruby on rails online course

    ReplyDelete