ruby-uebungen/Assignment_031/solution/readme.md

21 lines
568 B
Markdown
Raw Normal View History

2023-05-25 17:47:19 +02:00
# Lösung: Reguläre Ausdrücke: Matching
2023-05-23 09:19:31 +02:00
2023-05-25 17:47:19 +02:00
<div style="border: 1px solid grey;"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>```ruby
def reverser(s)
/(.*) (.*)/ =~ s
"#{$2} #{$1}"
end
puts reverser("Ruby Programmierer")
```
Alternative Lösung:
```ruby
def reverser(s)
m = /(.*) (.*)/.match(s)
"#{m[2]} #{m[1]}"
end
puts reverser("Ruby Programmierer")
2023-05-23 09:19:31 +02:00
```