Ruby - 从符号调用方法

时间:2016-03-06 10:14:46

标签: ruby-on-rails ruby

我有一种方法来设置货币的区域设置。

def local_number_to_currency(u)
      number_to_currency u,
        separator: ".",
        delimiter: ",",
        precision: 0,
        locale: :fr,
        raise: true  
    end

我想让:fr成为一个变量,以便根据一个名为Deal的模态'Country'属性的属性设置变量

def extract_locale_from_country(country)       
    COUNTRY_TO_YML_LOCALE_CODE[country] || DefaultLocale
  end

def local_number_to_currency(u)
      number_to_currency u,
        separator: ".",
        delimiter: ",",
        precision: 0,
        locale: :#{extract_locale_from_country(deal.country)},
        raise: true  
    end

当然行locale: :#{extract_locale_from_country(deal.country)}不起作用。我不知道如何在ruby中用:fr替换:{call amethod here}

如何实现?

2 个答案:

答案 0 :(得分:3)

您应该将String显式转换为Symbol

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

// function generator:
double RandomNumber () { return (std::rand() / 10e+7); }

int main() {

    using namespace std;
    std::srand ( unsigned ( std::time(0) ) );

    // 5000 is representative
    int n = 5000;

    std::vector<double> x (n);
    std::generate (x.begin(), x.end(), RandomNumber);

    std::vector<double> y (n);
    std::generate (y.begin(), y.end(), RandomNumber);

    for(std::vector<double>::const_iterator i = x.begin(); i != x.end(); i++) {
    y.push_back(*i);
}

    std::sort(x.begin(), x.end());
    std::sort(y.begin(), y.end());

    return 0;
}

答案 1 :(得分:0)

您可以使用Location.world - methode直接将字符串转换为符号。

to_sym

但是你应该小心将用户输入转换成符号,因为&#34;年龄较大&#34; ruby版本(&lt; 2.2)不是gc符号。

Symbol GC in Ruby 2.2