PHP developer still writing PHP in Ruby/Rails

Hi,

I’m a former PHP developer moving to Ruby and Rails. I’ve written a
little
rake task to provide an interactive console for setting and removing
session
values to help debugging the site. It all works, only thing is that it
feels
to me like I´ve written Ruby in PHP style but I don’t know where to
start
fixing it. I would appreciate any suggestions.

I´ve put it into pastie at: http://pastie.caboo.se/98992 but I’ve also
copied it below.

In my generic debugging rake tasks file:

desc ‘View and edit session data’
task :sessions => :environment do
# Message used when finding sessions
msg_session = <<-EOF
** SESSION CHECKER
Enter a session id to find the session in the format of
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
/(q)uit
EOF

# When viewing a session
msg_view = <<-EOF

Commands:
/(v)iew to view all values in the session
= to set a value
(d) to delete that field from the session
(f)ind to search for a new session
(q)uit to exit
EOF

# Begin session finding loop
loop do
  breaker = nil

  # Output the message
  puts msg_session
  print '> '

  # What are we doing?
  str = STDIN.gets.chomp
  break if str.empty? || str =~ /^q$/i

  # Does this session exist?
  if CGI::Session::ActiveRecordStore::Session.exists? :session_id => 

str
session_id = str
watch = []

    loop do
      session = CGI::Session::ActiveRecordStore::

Session.find_by_session_id session_id

      # Display the session data sorted, with the modified fields 

shown
in green
longest_key = 0
session.data.each { |k,v| longest_key = k.to_s.length if
k.to_s.length > longest_key }

      puts "\nAll variables in session: %s" % session.session_id
      session.data.each do |k,v|
        str = "  :%-#{longest_key}s => %s" % [k, 

CfSimple.describe_var
(v)]
puts watch.member?(k) ? green(str) : str
end

      # What are we doing with this item?
      puts msg_view
      print '> '

      str = STDIN.gets.chomp
      next if str.empty? || str =~ /^v$/i
      breaker = :quit and break if str =~ /^q$/i
      breaker = :find and break if str =~ /^f$/i

      if str =~ /^d ([a-z0-9 _]+)/i
        puts red('Deleted %s' % $1)
        session.data.delete $1.intern
        session.save
        next
      end

      # We're updating a value
      if str =~ /([^=]+)=(.*)/
        # Is the assignment valid yaml?
        valid = true
        YAML::parse(':%s: %s' % [$1, $2]) rescue valid = false

        # Not good YAML
        puts(red('Invalid assignment. Must be valid YAML.')) and 

next
unless valid

        # Now enter this as our new data
        session.data[$1.intern] = YAML::parse($2).transform
        session.save

        # Add it to the watches
        watch << $1.intern

        next
      else
        puts red('Invalid assignment. Must match: /([^=]+)=(.*)/')
      end
    end
  else
    puts 'Error finding session %s' % str
  end

  break if breaker == :quit
end

end

CfSimple is a library of basic debugging functions. I’ve just include

the
one in use above.

class CfSimple

def self.describe_var(var)
return (var.nil? ? ‘nil’ : var) if [NilClass, TrueClass,
FalseClass].member? var.class

  vals = [var.class.to_s + (!var.nil? && var.respond_to?(:length) ?

‘(%s)’ % var.length : ‘’) + ‘:’]

  if var.is_a? Array
      vals << '[%s]' % var.collect{ |a| describe_var a }.join(', ')
  elsif var.is_a? Hash
      vals << '{%s}' % var.collect{ |a| describe_var a }.join(', ')
    elsif var.is_a? Numeric
        vals << '%s' % var
    else
        vals << '"%s"' % var
    end

  vals.join ' '
end

end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs