ruby-uebungen/Assignment_035/solution/readme.md

21 lines
568 B
Markdown
Raw Normal View History

2023-05-23 09:19:31 +02:00
# Lösung: Reguläre Ausdrücke: Matching
<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")
```