DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Ruby (http://www.daniweb.com/forums/forum73.html)
-   -   My first Ruby programming question :-) (http://www.daniweb.com/forums/thread102441.html)

tefflox Dec 26th, 2007 12:36 am
My first Ruby programming question :-)
 
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 ---

<?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 (?)

tefflox Dec 26th, 2007 5:57 am
Re: My first Ruby programming question :-)
 
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.

pty Dec 29th, 2007 3:04 pm
Re: My first Ruby programming question :-)
 
Quote:

Originally Posted by tefflox (Post 498336)
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:

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") %>

eruder Jan 1st, 2008 1:40 pm
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

tefflox Jan 1st, 2008 1:49 pm
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.

pty Jan 2nd, 2008 11:40 am
Re: My first Ruby programming question :-)
 
Quote:

Originally Posted by eruder (Post 501855)
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 :)


All times are GMT -4. The time now is 3:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC