我正在尝试生成一些Ruby代码,它将获取一个字符串并返回一个新的字符串,从其末尾删除了一些x个字符 - 这些可以是实际的字母,数字,空格等.
例如:给出以下字符串
 a_string = "a1wer4zx" 
我需要一种简单的方法来获得相同的字符串,减去 - 比如说 - 最后3个字符.在上面的例子中,那将是"a1wer".我现在这样做的方式似乎很复杂:
an_array = a_string.split(//,(a_string.length-2))
an_array.pop
new_string = an_array.join
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我在Cartopy中绘制世界地图,作为整个过程的一部分,我想将所有线条涂成白色.除了地图的边界外,我已经能够为每个元素做到这一点.这是我正在使用的代码:
import matplotlib.pyplot as plt
import cartopy
import cartopy.io.shapereader as shpreader
import cartopy.crs as ccrs
fig = plt.Figure()
fig.set_canvas(plt.gcf().canvas)
ax = plt.axes(projection=ccrs.PlateCarree())
ax.add_feature(cartopy.feature.LAND, linewidth=0.5, edgecolor='white')
ax.set_extent([-150,60,-25,60])
shpf = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries')
reader = shpreader.Reader(shpf)
countries = reader.records()
for country in countries:
    ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0,1,0), linewidth=0.5, edgecolor='white', label=country.attributes['adm0_a3'])
fig.savefig("final_image.png", format='png', dpi=200)
Run Code Online (Sandbox Code Playgroud)
这是最终的结果:
想知道如何将边界线改为白色或完全关闭,尽可能少的步骤?
谢谢!
我有一个由脚手架创建的现有实体.现在我意识到我并不需要提供节目视图,因此我想要消除现在不必要的任何方法和部分.除了show在对象控制器上为我创建的方法,我应该删除哪些其他部分?这些是我能想到的:
show.html.erb实体的文件link_to对index.html.erb和edit.html.erb文件的对象实例的引用redirect_to对更新调用,并创建在控制器方法还有什么我应该删除的吗?