# PHP developer still writing PHP in Ruby/Rails

**URL:** <https://www.ruby-forum.com/t/php-developer-still-writing-php-in-ruby-rails/115080>\
**Category:** Rails\
**Created:** [September 20, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/php-developer-still-writing-php-in-ruby-rails/115080 "2007-09-20T12:53:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morgan\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/57b2e6/32.png) [@Morgan\_G](https://www.ruby-forum.com/u/Morgan_G)\
**Post date:** [September 20, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/php-developer-still-writing-php-in-ruby-rails/115080/1 "2007-09-20T12:53:51Z")

</div>

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: [Parked at Loopia](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
