Jump to content

Convert Ruby to PHP


Lassie

Recommended Posts

I want to convert a program from what I think is Ruby to PHP as I have no knowledge of Ruby.

Could someone confirm if the posted code is Ruby please and if there are any known methods to help with a conversion.

Is it possible to run Ruby code within PHP?

The background to this request is that I have a 3 axis accelerometer and would like to construct a pedometer.

I have the data logged to a file but coding the pedometer is a challenge.

I found the following ,A Pedometer in the Real World

Dessy Daskalov' which does what I want but I dont follow the code.
For those interested a link to the article:-http://www.aosabook.org/en/500L/a-pedometer-in-the-real-world.html
Any guidance appreciated and I hope this is the right forum.
 
class Parser

  attr_reader :parsed_data

  def self.run(data)
    parser = Parser.new(data)
    parser.parse
    parser
  end

  def initialize(data)
    @data = data
  end

  def parse
    @parsed_data = @data.to_s.split(';').map { |x| x.split('|') }
                   .map { |x| x.map { |x| x.split(',').map(&:to_f) } }

    unless @parsed_data.map { |x| x.map(&:length).uniq }.uniq == [[3]]
      raise 'Bad Input. Ensure data is properly formatted.'
    end

    if @parsed_data.first.count == 1
      filtered_accl = @parsed_data.map(&:flatten).transpose.map do |total_accl|
        grav = Filter.low_0_hz(total_accl)
        user = total_accl.zip(grav).map { |a, b| a - b }
        [user, grav]
      end

      @parsed_data = @parsed_data.length.times.map do |i|
        user = filtered_accl.map(&:first).map { |elem| elem[i] }
        grav = filtered_accl.map(&:last).map { |elem| elem[i] }
        [user, grav]
      end
    end
  end

end

 

Link to comment
Share on other sites

Yes, this is Ruby. No, you cannot run Ruby within PHP, and there's no way to automatically translate one language into another.

 

I see two possibilities:

  • You try to understand the concept and then write your own PHP implementation from scratch.
  • You pay somebody to do a manual translation. This will be expensive. There aren't many programmers who are proficient in both Ruby and PHP, and the job is tedious rather than challenging.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.