how is the string reverse function implemented in ruby [duplicate] - ruby

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Reverse a string in Ruby
in python using extended slice , an implementation for string reverse can be written like this
>>> 'hello world'[::-1]
'dlrow olleh'
i wonder how would one implement string reverse function in Ruby

>> "Hello world".reverse
=> "dlrow olleH"

Related

Ruby: Can & operator work with several symbols at once? [duplicate]

This question already has answers here:
Chaining methods using Symbol#to_proc shorthand in Ruby?
(4 answers)
Closed 5 years ago.
For example, I have enum.map(&:join).map(&:to_i). Is there a syntax in Ruby where I can write something like this: enum.map(&:join:to_i) in order to avoid iterating through the array twice using & operator?
You could use a block (with no &) to iterate just once:
enum.map { |e| e.join.to_i }

how to write better code using java8 [duplicate]

This question already has answers here:
Lambda expression to convert array/List of String to array/List of Integers
(10 answers)
Closed 6 years ago.
I am converting my project to java8. How would I write this code in a better way using java8?
List<Bar> bars = new ArrayList<>();
for (Foo foo : obj.getFooList()) {
bars.add(Helper.fooToBar(foo));
}
return detailsVos;
Stream the list, mapping using a method reference, then collect to a list and return it:
return obj.getFooList().stream().map(Helper::fooToBar).collect(Collectors.toList());
Note that "better" has been interpreted as "neater" and "using the Java 8 style".
Also note that this may perform slightly worse than your original code, due to the overhead of using a stream.

What does ? do in this Ruby expression? [duplicate]

This question already has answers here:
What are the restrictions for method names in Ruby?
(5 answers)
Closed 7 years ago.
On the Chef Style Guide page appears this Ruby expression:
antarctica_hint = hint?('antarctica')
What exactly does the ? after hint and before ('antarctica') mean? Is it just part of the method name? (i.e. the method is called 'hint?' not 'hint')
It is part of method name, and people typically (not always) use it for methods that return boolean value.
An example from Ruby is Class#respond_to?

Ruby * operator before array [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Understanding ruby splat in ranges and arrays
could anyone tell me what the * does in the following piece of code?
line = "name=yabbi;language=ruby;"
Hash[*line.split(/=|;/)]
Thanks.
* is the splat operator. It is used to split an array into a list of arguments.
line.split(/=|;/) returns an array. To create a Hash, each element of the array must be passed as an individual parameter.
it's a splat operator Read about it. Often times you see it used when you want to split up an array to use as parameters of a function.

What does a single splat/asterisk in a Ruby argument list mean? [duplicate]

This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 10 years ago.
I was poking through the Rails 3 ActiveRecord source code today and found a method where the entire parameter list was a single asterisk.
def save(*)
I couldn't find a good description of what this does (though I have some ideas based on what I know about splat arguments).
What does it do, and why would you use it?
It means it can have any number of arguments (including zero) and it discards all those arguments.

Resources