943,696 Members | Top Members by Rank

Ad:
  • Ruby Discussion Thread
  • Marked Solved
  • Views: 3743
  • Ruby RSS
Dec 26th, 2007
0

My first Ruby programming question :-)

Expand Post »
Hello, I'm pleased to be here by way of the html forum. I've been using php for static pages for 18 months. I wanted to learn rails, but I just realized that I could configure my host to automate index.rhtml files just like php. This is a major motivator for me to learn Ruby, and after a few more months I can start fresh with Rails. OK, here is the code that works in PHP, and what I've got so far in Ruby. The JS effect can be viewed here: http://listenlight.net/13 ---

Ruby Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $i = 0; $n = 0;
  4.  
  5. $str = "listenlightpoetryjournal";
  6.  
  7. while( $str[$i] != "" ) {
  8. echo("<span id=\"n".$n++."\" style=\"position:relative; visibility: hidden; font-size: normal; top:".(rand(-18, 18))."px; color: black;\">".$str[$i]."</span>");
  9.  
  10. $i++;
  11. }
  12.  
  13.  
  14. ?>

Ruby Syntax (Toggle Plain Text)
  1. <%
  2. def banner
  3. str = "listenlightpoetryjournal"
  4. i, n = 0, 0
  5. until c = str[i] == nil
  6. line = "<span id=\"n", n, "\" style=\"position: relative; font-size: normal; top:", rand(36) - 18, "px;\">", c, "</span>"
  7. i += 1
  8. n += 1
  9. output = output, line
  10. end
  11. end
  12. %>
  13.  
  14. <%= banner %>

In the Ruby example, I've set the visibility attribute in the CSS, attempting to clean the markup. Also, is there a way to do a parallel incrementing?--such as a, b += 1, 1 (?)
Similar Threads
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Dec 26th, 2007
0

Re: My first Ruby programming question :-)

My solution ---

Ruby Syntax (Toggle Plain Text)
  1. <%=
  2. def banner
  3. i, n, span, str = 0, 0, String.new, "listenlightpoetryjournal"
  4. until i == str.length do
  5. span << "<span id=\"n" << n.to_s << "\" style=\"top: "
  6. << (rand(36) - 18).to_s << "px;\">" << str[i] << "</span>"
  7. i, n = i + 1, n + 1
  8. end
  9. return span
  10. end
  11. banner
  12. %>

No more PHP for this one.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Dec 29th, 2007
0

Re: My first Ruby programming question :-)

Click to Expand / Collapse  Quote originally posted by tefflox ...
My solution ---

Ruby Syntax (Toggle Plain Text)
  1. <%=
  2. def banner
  3. i, n, span, str = 0, 0, String.new, "listenlightpoetryjournal"
  4. until i == str.length do
  5. span << "<span id=\"n" << n.to_s << "\" style=\"top: "
  6. << (rand(36) - 18).to_s << "px;\">" << str[i] << "</span>"
  7. i, n = i + 1, n + 1
  8. end
  9. return span
  10. end
  11. banner
  12. %>

No more PHP for this one.
Although your solution is fine it can be a little more readable (and re-usable) in rails.

In the controller:

ruby Syntax (Toggle Plain Text)
  1. def banner(text)
  2. span = String.new
  3. text.split(//).each_with_index do |c, i|
  4. span += "<span id='#{i}' style='top:#{(rand(36)-18)}px;'>#{c}</span>\n"
  5. end
  6. span
  7. end

Then in the view

Ruby Syntax (Toggle Plain Text)
  1. <%= banner("listenlightpoetryjournal") %>
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Jan 1st, 2008
0

Re: My first Ruby programming question :-)

pty's improvement is a good one!

The only nit I have with it is that I wouldn't put the banner method in the controller, since it has nothing to do with the "traffic cop" jobs that controllers normally do. I would put it in a helper file, either this controller's helper file or the application_helper.rb file (if it's a method you'd want to use in other controllers).

Ed
Reputation Points: 10
Solved Threads: 1
Newbie Poster
eruder is offline Offline
3 posts
since Jan 2005
Jan 1st, 2008
0

Re: My first Ruby programming question :-)

thanks. i appreciate the direction on working with strings, but it's only for static pages. i'm not using the rails framework. i just wanted to make it concise.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Jan 2nd, 2008
0

Re: My first Ruby programming question :-)

Click to Expand / Collapse  Quote originally posted by eruder ...
pty's improvement is a good one!

The only nit I have with it is that I wouldn't put the banner method in the controller, since it has nothing to do with the "traffic cop" jobs that controllers normally do. I would put it in a helper file, either this controller's helper file or the application_helper.rb file (if it's a method you'd want to use in other controllers).

Ed
Thanks Ed
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Ruby Forum Timeline: Multiple db tables in Rails...
Next Thread in Ruby Forum Timeline: Simple Rails Application trouble





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC