2013-11-25 6 views

답변

2

내가 찾아 그 rpartition 작품을 아주 잘이 상황 :

s = 'hello/world/of/stacks' 

p s.rpartition('/').first #=> "hello/world/of" 

또는, 공상 수 있도록하려면 :

s, = s.rpartition('/') 

p s #= > "hello/world/of" 
+0

니스 (+1). 루비 클래스에는 상상할 수있는 단일 함수에 대한 메서드가 있다고 생각합니다. ;) – lurker

1

사용 rindex[] 방법 : 다음 사용

input.str[0, input.rindex(?/)] 
+1

그냥 문자열'로 실수하지 않습니다 [a..b]'. 그 범위에서 시작됩니다. 길이에서 오는'string [a, b] '에 대해서 말하고 있습니다. – quetzalcoatl

0
1.9.3p448 :024 > str = "hello/world/of/stacks" 
=> "hello/world/of/stacks" 

그런 다음 rindex를 사용하여 문자열에서 슬래시의 마지막 인덱스를 찾을 수 있습니다 ...

1.9.3p448 :025 > str.rindex("/") 
=> 14 

색인을 사용하면 해당 슬래시까지 최대 문자 만 가져올 수 있습니다 ...

1.9.3p448 :026 > str[0..(14 - (str.length + 1))] 
=> "hello/world/of" 
1
File.dirname("hello/world/of/stacks") 
# => "hello/world/of" 
+0

실제로 작업하는 경로 인 경우 이동하는 방법입니다. – hirolau

관련 문제