•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Ruby section within the Web Development category of DaniWeb, a massive community of 361,584 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,074 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Ruby advertiser: SELL YOUR PRODUCT TODAY !
Views: 1726 | Replies: 5 | Solved
![]() |
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 ---
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 (?)
<?php
$i = 0; $n = 0;
$str = "listenlightpoetryjournal";
while( $str[$i] != "" ) {
echo("<span id=\"n".$n++."\" style=\"position:relative; visibility: hidden; font-size: normal; top:".(rand(-18, 18))."px; color: black;\">".$str[$i]."</span>");
$i++;
}
?> <%
def banner
str = "listenlightpoetryjournal"
i, n = 0, 0
until c = str[i] == nil
line = "<span id=\"n", n, "\" style=\"position: relative; font-size: normal; top:", rand(36) - 18, "px;\">", c, "</span>"
i += 1
n += 1
output = output, line
end
end
%>
<%= 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 (?)
My solution ---
No more PHP for this one.
<%=
def banner
i, n, span, str = 0, 0, String.new, "listenlightpoetryjournal"
until i == str.length do
span << "<span id=\"n" << n.to_s << "\" style=\"top: "
<< (rand(36) - 18).to_s << "px;\">" << str[i] << "</span>"
i, n = i + 1, n + 1
end
return span
end
banner
%>No more PHP for this one.
•
•
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation:
Rep Power: 3
Solved Threads: 30
•
•
•
•
My solution ---
<%= def banner i, n, span, str = 0, 0, String.new, "listenlightpoetryjournal" until i == str.length do span << "<span id=\"n" << n.to_s << "\" style=\"top: " << (rand(36) - 18).to_s << "px;\">" << str[i] << "</span>" i, n = i + 1, n + 1 end return span end banner %>
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)
def banner(text) span = String.new text.split(//).each_with_index do |c, i| span += "<span id='#{i}' style='top:#{(rand(36)-18)}px;'>#{c}</span>\n" end span end
Then in the view
<%= banner("listenlightpoetryjournal") %> Note to self... pocket cup
•
•
Join Date: Jan 2005
Location: San Jose, CA
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 1
pty's improvement is a good one!
The only nit I have with it is that I wouldn't put the
Ed
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
•
•
Join Date: Oct 2005
Location: Manchester, UK
Posts: 481
Reputation:
Rep Power: 3
Solved Threads: 30
•
•
•
•
pty's improvement is a good one!
The only nit I have with it is that I wouldn't put thebannermethod 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 theapplication_helper.rbfile (if it's a method you'd want to use in other controllers).
Ed
Thanks Ed
Note to self... pocket cup
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Ruby Marketplace
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google high-performance innovation java languages linerider mcse microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials xml
- _VB or C#_ (ASP.NET)
- Is It Worth Learning Rugby? (Ruby)
- 100% New! (Community Introductions)
- Newbie to PHP question (PHP)
- very 1st python tutorial 4 newbies (Python)
Other Threads in the Ruby Forum
- Previous Thread: Multiple db tables in Rails...
- Next Thread: Simple Rails Application trouble


Linear Mode