Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write this so it's easier to understand?

Tags:

ruby

Is there and way to write code like this in a way that makes what it does clearer?

a = (a.split(" ")[1..-1]).join(" ")

That deletes the first word of a sentence but the code doesn't look expressive at all.

like image 533
vikhyat Avatar asked Nov 23 '25 09:11

vikhyat


1 Answers

irb(main):024:0> "this is a test".split.drop(1) * " "
=> "is a test"

Edited to add:

Explanation:

  • By default #split delimits on whitespace.

  • #drop(1) gets rid of the first entry.

  • * " " does the same as #join(" ").

like image 142
JUST MY correct OPINION Avatar answered Nov 24 '25 21:11

JUST MY correct OPINION



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!