The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Mixer Remixed

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Joey Gibson

Posts: 71
Nickname: joeygibson
Registered: Jun, 2003

Joey Gibson is an opinionated software architect who blogs about technology, politics, music, etc.
Mixer Remixed Posted: Sep 17, 2003 9:02 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Joey Gibson.
Original Post: Mixer Remixed
Feed Title: Joey Gibson's Blog
Feed URL: http://www.jobkabob.com/index.html
Feed Description: Thoughts, musings, ramblings and rants on Java, Ruby, Python, obscure languages, politics and other exciting topics.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Joey Gibson
Latest Posts From Joey Gibson's Blog

Advertisement
After reading several threads about the text scrmabler and various implementations, I revised my Ruby version and it's now 21 lines shorter and much more Ruby-like. It makes far less of an attempt to deal with punctuation, but I think that's OK. This is just a lark, after all.

 1  class Mixer
 2      private
 3      def randomize(str)
 4          return str if str.length < 4
 5
 6          str =~ /\B.*\B/
 7          first = $`
 8          last = $'
 9          first + ($&.split(//).sort_by {rand}.join) + last
10      end
11
12      public
13      def mix_file(filename)
14          lines = IO.readlines(filename)
15
16          mix_lines(lines)
17      end
18
19      def mix_string(str)
20          mix_lines(str.split).join(" ")
21      end
&l
1000
t;font color="#804040">22
23      def mix_lines(lines)
24          lines.collect! do |line|
25              words = Array.new
26
27              line.split(/\W/).each do |word|
28                  words << randomize(word)
29              end
30
31              words.join(" ")
32          end
33      end
34  end

Read: Mixer Remixed

Topic: Design Patterns in Ruby (in German) Previous Topic   Next Topic Topic: Ruby 1.8.0 Released

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use