dongluanguang4774 2016-02-13 15:39
浏览 65
已采纳

如何将这个PHP代码转换为ruby(RoR)代码? [关闭]

I am new in ruby on rails. I am migrating from php to ruby. Now I have some php projects which are being converted into ruby code.But How can I convert this switch code to ruby on rails 4? function ajax($command) {

    switch ($command) {
        case 'page_reload':
            $this->ajax_delete_entries_of_current_uid();
            break;

        case 'labchem_products':
            $this->ajax_labchem_products();
            break;

        case 'labchem_carts':
            $this->ajax_labchem_carts();
            break;

        case 'labchem_customers':
            $this->ajax_labchem_customers();
            break;

        case 'products_selected':
            $this->ajax_products_selected();
            break;

        case 'products_total':
            $this->ajax_products_total();
            break;

        case 'products_delivery_info':
            $this->ajax_products_delivery_info();
            break;

        case 'labchem_orders':
            $this->ajax_labchem_orders();
            break;

        default: break;
    }
} 
  • 写回答

1条回答 默认 最新

  • dorv56831 2016-02-13 15:55
    关注
    case command
      when 'page_reload'      then ajax_delete_entries_of_current_uid()
      when 'labchem_products' then ajax_labchem_products()
      # or
      when 'labchem_carts'
        ajax_labchem_carts()
      # and so on ...
    end
    

    You don't need break. Only one or no when will be executes. If no when matches you could note an else to execute something.

    In ruby the case will return the last value, so you could save it to a variable.

    result =
      case command
        when 'a', 'b' then 1
        when 'c' then 2
        when 'd'..'z' then 3
        else
          0
      end
    

    The comparison is done by object type and value (===).

    case 1
      when '1' then 'a'
      when 1 then 'b'
    end
    # => "b"
    

    More at ruby-doc.com and tutorialspoint.com.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)