2014-07-14 51 views
0

首先,我對可能在此問題中顯示的任何不正確的術語或常見的ruby/rails/html誤解表示歉意,因爲我仍然在學習語言以及它們如何工作。我已經得到了一個gui來處理和修復從rails 3.0升級到3.2之後的錯誤,我在很多地方看到了工件。Rails將字符串顯示爲數組

具體而言,我在這篇文章中要求的是一個字符串顯示問題。我擁有的簡短版本是在網頁中顯示的消息線索,該網頁看起來接近您在智能手機上的大多數短信客戶端中現在看到的內容。如果系統知道他們是誰(在我們這裏註冊),那麼該頁面會顯示一個人的姓名,如果他們不是,則顯示一個手機號碼。

然而,問題是不是顯示5554440002,而是顯示爲:「」(「,」5「,」5「,」5「,」)「,」「,」4「,」4「 ,「4」,「 - 」,「0」,「0」,「0」,「1」]

環顧網絡,並在堆棧溢出我發現this post要成爲最接近回答我的問題。然而,刪除= <%=的建議在手機號碼中不再顯示。

組合,其產生該輸出代碼的行是:

<%= find_associated_account conversation_message['from'] -%> 

編輯1(定義):

def find_associated_account(input) 
    if input[0..0] == $operator.domestic_code.to_s 
     input = input[1..10] 
    end 
    if @account.associated_accounts and @account.associated_accounts.count > 0  
     if !(input.match(/@/)) && !(input.match(/MISSING_MAILBOX/)) 
     input = input[0..9] 
     end 
     begin 
     acc = @account.associated_accounts.find_by_number(input) 
     if acc 
      return get_display_name_for_account(acc) 
     end 
    end 
    return format_mdn(input) 
    end 

    def format_mdn(input) 
    domestic_length = $operator.domestic_mask.count('#') 
    number_cc_stripped = input.sub(/\A[#{$operator.domestic_code}]/, '') 
    if /^[0-9]{#{domestic_length}}$/.match(number_cc_stripped) 
     result = [] 
     count = 0 
     $operator.domestic_mask.each_char do |c| 
     if c == '#' 
      result << number_cc_stripped[count,1] 
      count += 1 
     else 
      result << c 
     end   
     end 
     return result.to_s 
    end 
    return input 
    end 

給出的任何建議,將不勝感激並將提供所需要的任何其他信息。

+0

,如果你給我們提供的這種方法定義這將是偉大的... – ryaz

+0

與format_mdn – MumblesCrzy

+0

'result.to_s返回值搞亂後解決了它'會給你一個數組的字符串表示形式,如果你想把它變成一個真正的字符串,'.join(「」)' –

回答

0

修正了這與format_mdn的返回值混淆之後。我改變了結果的初始化爲「」而不是[],我猜想改變了變量的類型?在任何情況下這個定義的最終代碼:

def format_mdn(input) 
    domestic_length = $operator.domestic_mask.count('#') 
    number_cc_stripped = input.sub(/\A[#{$operator.domestic_code}]/, '') 

    if /^[0-9]{#{domestic_length}}$/.match(number_cc_stripped) 
    result = [] 
    count = 0 
    $operator.domestic_mask.each_char do |c| 
     if c == '#' 
     result << number_cc_stripped[count,1] 
     count += 1 
     else 
     result << c 
     end   
    end 
    return result.join("") 
    end 
    result = number_cc_stripped 
    return result 
end 
相關問題