我需要一些帮助来操作Ruby中的字符串

时间:2016-12-21 05:14:47

标签: arrays ruby string class constants

我有这个字符串'custom_controller'。

我需要把它带到'CustomController'。

它来自所需的文件名,需要内部的类名。

要切入追逐,代码为here

这是一个来自Github的Rack教程的项目。 rack/rack/wiki/Tutorials

但是,不仅仅将它们命名为custom.rb和类Custom,我希望它更像Rails应用程序而不需要Rails或Rails附带的任何宝石,除了Rack。 这不是Rails应用程序。这甚至不是Sinatra或Padrino应用程序。它只是一个使用Rack的家庭酿造网络框架。

4 个答案:

答案 0 :(得分:2)

您可以使用普通红宝石来构建自己的CAST方法。

camelize

答案 1 :(得分:2)

这也有效:

<div id="facilities_suggestions_list" class="suggestions_list">
   <h3>Experience</h3>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120063:-122.595:37.9038889::CA&quot;, &quot;120063&quot;, 1 )" id="suggestion_1_120063"><strong>MOUNT</strong> <strong>TAMALPAIS</strong> <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120061:-121.93:37.8627778::CA&quot;, &quot;120061&quot;, 1 )" id="suggestion_1_120061"><strong>MT.</strong> DIABLO <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120062:-116.675:33.8102778::CA&quot;, &quot;120062&quot;, 1 )" id="suggestion_1_120062"><strong>MT.</strong> SAN JACINTO <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:70045:-106.2227778:38.7141667::CO&quot;, &quot;70045&quot;, 1 )" id="suggestion_1_70045"><strong>MOUNT</strong> PRINCETON <span>, CO</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:70124:-111.6036111:40.4061111::UT&quot;, &quot;70124&quot;, 1 )" id="suggestion_1_70124"><strong>MT.</strong> TIMPANOGOS <span>, UT</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281003:-81.2802778:29.8772222::FL&quot;, &quot;281003&quot;, 1 )" id="suggestion_1_281003">ANASTASIA <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120013:-122.3022222:37.2227778::CA&quot;, &quot;120013&quot;, 1 )" id="suggestion_1_120013">BUTANO <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NE:230042:-103.0197222:42.7091667::NE&quot;, &quot;230042&quot;, 1 )" id="suggestion_1_230042">Chadron <strong>SP</strong> <span>, NE</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120035:-120.065:34.4622222::CA&quot;, &quot;120035&quot;, 1 )" id="suggestion_1_120035">GAVIOTA <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120048:-121.5694444:36.0530556::CA&quot;, &quot;120048&quot;, 1 )" id="suggestion_1_120048">LIMEKILN <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120049:-123.7925:39.4933333::CA&quot;, &quot;120049&quot;, 1 )" id="suggestion_1_120049">MACKERRICHER <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120052:-123.7113889:38.9738889::CA&quot;, &quot;120052&quot;, 1 )" id="suggestion_1_120052">MANCHESTER <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281060:-82.6033333:29.9194444::FL&quot;, &quot;281060&quot;, 1 )" id="suggestion_1_281060">OLENO <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NE:230143:-96.7172222:42.6097222::NE&quot;, &quot;230143&quot;, 1 )" id="suggestion_1_230143">Ponca <strong>SP</strong> <span>, NE</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281076:-81.0836111:29.3408333::FL&quot;, &quot;281076&quot;, 1 )" id="suggestion_1_281076">TOMOKA <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281077:-84.9505556:30.5697222::FL&quot;, &quot;281077&quot;, 1 )" id="suggestion_1_281077">TORREYA <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120097:-123.7733333:39.2763889::CA&quot;, &quot;120097&quot;, 1 )" id="suggestion_1_120097">VANDAMME <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:71897:-131.8988889:56.6441667::AK&quot;, &quot;71897&quot;, 1 )" id="suggestion_1_71897"><strong>MOUNT</strong> FLEMER CABIN <span>, AK</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:75021:-82.7566667:35.4027778::NC&quot;, &quot;75021&quot;, 1 )" id="suggestion_1_75021"><strong>MOUNT</strong> PISGAH CAMPGROUND <span>, NC</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:70459:-119.8973611:39.3122778::CA&quot;, &quot;70459&quot;, 1 )" id="suggestion_1_70459"><strong>MOUNT</strong> ROSE (NV) <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NRSO:71896:-132.2391667:56.6644444::AK&quot;, &quot;71896&quot;, 1 )" id="suggestion_1_71896"><strong>MOUNT</strong> RYNDA CABIN <span>, AK</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120003:-122.4308333:37.8641667::CA&quot;, &quot;120003&quot;, 1 )" id="suggestion_1_120003">ANGEL ISLAND <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281005:-81.2741667:24.6597222::FL&quot;, &quot;281005&quot;, 1 )" id="suggestion_1_281005">BAHIA HONDA <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281128:-87.4038889:30.3180556::FL&quot;, &quot;281128&quot;, 1 )" id="suggestion_1_281128">BIG LAGOON <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281008:-86.8783333:30.7116667::FL&quot;, &quot;281008&quot;, 1 )" id="suggestion_1_281008">BLACKWATER RIVER <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281101:-81.3338889:28.9508333::FL&quot;, &quot;281101&quot;, 1 )" id="suggestion_1_281101">Blue Spring <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281010:-82.8197222:28.0311111::FL&quot;, &quot;281010&quot;, 1 )" id="suggestion_1_281010">CALADESI ISLAND <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120016:-122.3505556:41.1711111::CA&quot;, &quot;120016&quot;, 1 )" id="suggestion_1_120016">CASTLE CRAGS <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120017:-121.1866667:37.6933333::CA&quot;, &quot;120017&quot;, 1 )" id="suggestion_1_120017">CASWELL MEMORIAL <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281127:-82.2452778:26.6858333::FL&quot;, &quot;281127&quot;, 1 )" id="suggestion_1_281127">CAYO COSTA <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120018:-122.4605556:38.0008333::CA&quot;, &quot;120018&quot;, 1 )" id="suggestion_1_120018">CHINA CAMP <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:124900::::CA&quot;, &quot;124900&quot;, 1 )" id="suggestion_1_124900">CHINO HILLS <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120019:-122.8141667:39.0066667::CA&quot;, &quot;120019&quot;, 1 )" id="suggestion_1_120019">CLEAR LAKE <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281013:-81.5913889:25.9916667::FL&quot;, &quot;281013&quot;, 1 )" id="suggestion_1_281013">COLLIER-SEMINOLE <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281228:-80.985:24.7422222::FL&quot;, &quot;281228&quot;, 1 )" id="suggestion_1_281228">CURRY HAMMOCK <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120023:-116.5616667:32.9358333::CA&quot;, &quot;120023&quot;, 1 )" id="suggestion_1_120023">CUYAMACA RANCHO <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120029:-120.2419444:39.32::CA&quot;, &quot;120029&quot;, 1 )" id="suggestion_1_120029">DONNER MEMORIAL <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120031:-120.0930556:38.9544444::CA&quot;, &quot;120031&quot;, 1 )" id="suggestion_1_120031">EMERALD BAY <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281021:-85.5288889:30.7308333::FL&quot;, &quot;281021&quot;, 1 )" id="suggestion_1_281021">FALLING WATERS <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281241:-82.9347222:29.5861111::FL&quot;, &quot;281241&quot;, 1 )" id="suggestion_1_281241">FANNING SPRINGS <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281022:-81.2686111:29.6675::FL&quot;, &quot;281022&quot;, 1 )" id="suggestion_1_281022">FAVER-DYKES <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281025:-85.2133333:30.8088889::FL&quot;, &quot;281025&quot;, 1 )" id="suggestion_1_281025">FLORIDA CAVERNS <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281027:-81.4347222:30.67::FL&quot;, &quot;281027&quot;, 1 )" id="suggestion_1_281027">FORT CLINCH <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;NE:230077:-103.4941667:42.6866667::NE&quot;, &quot;230077&quot;, 1 )" id="suggestion_1_230077">Fort Robinson <strong>SP</strong> <span>, NE</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120209:-121.5013889:36.7608333::CA&quot;, &quot;120209&quot;, 1 )" id="suggestion_1_120209">FREMONT PEAK <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281033:-86.1566667:30.3294444::FL&quot;, &quot;281033&quot;, 1 )" id="suggestion_1_281033">GRAYTON BEACH <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281186:-86.4466667:30.3852778::FL&quot;, &quot;281186&quot;, 1 )" id="suggestion_1_281186">HENDERSON BEACH <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;CA:120040:-123.485:39.085::CA&quot;, &quot;120040&quot;, 1 )" id="suggestion_1_120040">HENDY WOODS <strong>SP</strong> <span>, CA</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281036:-81.5305556:27.4711111::FL&quot;, &quot;281036&quot;, 1 )" id="suggestion_1_281036">HIGHLANDS HAMMOCK <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
   <div class="facility_suggestion"><a href="javascript:UnifSearchEngine.selectResolvedAddress(&quot;FL:281037:-82.2269444:28.1430556::FL&quot;, &quot;281037&quot;, 1 )" id="suggestion_1_281037">HILLSBOROUGH RIVER <strong>SP</strong> <span>, FL</span></a>&nbsp;</div>
</div>

更接近Rails handles this issue的方式。我建议你看看Rails代码,因为它还处理你引用的简单案例的一些变化。例如,您将如何处理名称间隔控制器"custom_controller".gsub(/_*([a-z]+)/i) {$1.capitalize}

答案 2 :(得分:0)

所需的结果只是源字符串的驼峰表示。您可以尝试使用'custom_controller'.camelize

答案 3 :(得分:0)

您可以通过以下方式将其作为“CustomController”获取:

'custom_controller'.split('_').collect{|x| x.camelize}.join

如果您想将此作为课程使用,您可以执行以下操作:

'custom_controller'.split('_').collect{|x| x.camelize}.join.constantize

constantize 主要用于将任何字符串转换为类。 但我不确定这是否适用于控制器。您可以阅读更多相关信息here

详细了解 camelize here