Using each in Ruby - ruby

I am starting to learn ruby and am trying to figure out a way to if i have an array of 16 numbers, to print those numbers 4 at a time using only the each method. I can easily do this with a loop but i am lacking full understanding of the each method in Ruby so this gives me some trouble.
I have the following:
x=[*1..16]
index=0
x.each do |element|
puts element[index]
index=index+3
end
Now obviously this is completely wrong and i know that but i wanted to put something on here. A little advice would be great.
Thank you

A possible solution:
a = (1..16)
a.each_slice(4) { |s| p s[3] }
EDIT: If you want print 4 elements on one line, then skip to the next line and print the next 4 elements, etc
a = (1..16)
a.each_slice(4) { |s| puts s.join(' ') }
Or using each only
a = (1..16)
a.each { |i| print i.to_s + ' '; print "\n" if i % 4 == 0 }

Try each_slice http://ruby-doc.org/core-1.9.3/Enumerable.html#method-i-each_slice.
Using that would look something like
(1..16).each_slice(4) do |x|
puts x.inspect
end

Related

Join a ruby enumerator into a string

I have an instance of Enumerator::Generator that yields strings. I need to join them into a single string.
What's a good way of doing this? I notice that * doesn't work. I know I can .map {|x| x} first but that seems rather un-idiomatic
I think in this case I' might reach for inject/reduce (aliases for the same method, reduce as a name makes more sense, to me) with the + operator:
enum.reduce(:+)
# or, passing in a block
enum.reduce(&:+)
As a full example:
# never used Enumerator::Generator directly, but you called it out specifically
# in your question, and this seems to be doing the trick to get it working
enum = Enumerator::Generator.new do |y|
y.yield "ant"
y.yield "bear"
y.yield "cat"
end
p enum.reduce(&:+) # output: "antbearcat"
# crude example of modifying the strings as you join them
p enum.reduce('') { |memo, word| memo += word.upcase + ' ' }
# output: "ANT BEAR CAT "
a=["Raja","gopalan"].to_enum #let's assume this is your enumerator
Write the following code
p a.map(&:itself).join
Or
p a.to_a.join
output
"Rajagopalan"

Ruby modify a piece of a string

Totally new to Ruby. This is a simple homework assignment. The secret_code function needs to take in input string and perform the following actions:
In the first block of letters before a space, capitalize all but the first char
Reverse the string
So if the input were "super duper", the output should be "repud REPUs".
I coded the function as follows:
def secret_code(input)
input.split(" ").first[1..-1].each_char do |i|
input[i] = i.upcase
end
return input.reverse
end
It passes the unit tests, but I am wondering if there is a better way to code it. Is it possible to avoid using the loop? I tried
return input.split(" ").first[1..-1].upcase.reverse
But that didn't quite work. Any thoughts on how to clean this up are appreciated!
"super duper".sub(/(?<=.)\S+/, &:upcase).reverse
How about this:
def secret_code(input)
first_space = input.index(' ')
(input[0] + input[1...first_space].upcase + input[first_space..-1]).reverse
end
Note that in Ruby, the last expression evaluate in a method is always returned, so you can omit the final return.
s = "super duper"
words = s.split(' ')
words.first[1..-1] = words.first[1..-1].upcase
words.each { |word| word.reverse! }
s = words.reverse.join(' ')
puts s # => repud REPUs
Not necessarily any better, but sure, it can be done without a loop...
def f x
(b = [(a = x.split)[0].upcase, *a.drop(1)].join(' ').reverse)[-1] = x[0, 1]
return b
end
You can try the below:
a = "super duper"
p a.gsub(a.split[0...1].join(' '),a.split[0...1].join(' ').capitalize.swapcase).reverse
Output:
"repud REPUs"

Repeat Method to Give "string" an "x" amounts of times

I'm trying to write a method that will take two arguments, one for the string, and the other the number of times it will be repeated. here is the code of i have:
def repeat(text,c=2)
c.times do print text end
end
repeat ("hi")
problem here is, I want to have the result to be "hi hi"
i tried "puts" but that starts a new line...
[ print text " + " text ] doesn't work as well...
thanks for the help!
Your question is unclear. If all you want is to print the text repeated n times, use String#*
def repeat(text, n=2)
print text * n
end
Your example result says you want "hi hi" implying you would like spaces between each repetition. The most concise way to accomplish that is to use Array#*
def repeat(text, n=2)
print [text] * n * ' '
end
Simply multiply the string by the number, Ruby is smart enough to know what you mean ;)
pry(main)> "abcabcabc" * 3
=> "abcabcabcabcabcabcabcabcabc"
Or you could do something like:
def repeat(text, c=2)
print c.times.collect { text }.join(' ')
end
Enumerator#cycle returns an enumerator:
puts ['hi'].cycle(3).to_a.join(' ')
# => hi hi hi
Breaking down the code:
['hi'] creates an array containing a string
cycle(3) creates an enumerator from the array that repeats the elements 3 times
.to_a creates an array from the enumerator so that the join method of Array can create the final output string.
def repeat(text, c=2)
print Array.new(c, text).join(' ')
end
I am new to ruby, but I thought this solution worked well for me and I came up with it myself.
def repeat(word, i=2)
word + (" #{word}" * (i-1))
end
You can try this:
def repeat(text, c=2)
print ((text + ' ')*c).strip
end
def repeat(text, c=2)
print ([text]*c).join(' ')
end
Perhaps easier to read. Unless, is there any reason to use the .collect method instead?
I don't see the point in creating an array (with or without collect()) and then calling join(). This works too:
def repeat(text, c=2)
c.times { |i| print text; print ' ' unless i+1 == c }
end
Although, it is a little more verbose (which is arguably un-ruby like) it does less work (which maybe makes more sense).

escaping the .each { } iteration early in Ruby

code:
c = 0
items.each { |i|
puts i.to_s
# if c > 9 escape the each iteration early - and do not repeat
c++
}
I want to grab the first 10 items then leave the "each" loop.
What do I replace the commented line with? is there a better approach? something more Ruby idiomatic?
While the break solution works, I think a more functional approach really suits this problem. You want to take the first 10 elements and print them so try
items.take(10).each { |i| puts i.to_s }
There is no ++ operator in Ruby. It's also convention to use do and end for multi-line blocks. Modifying your solution yields:
c = 0
items.each do |i|
puts i.to_s
break if c > 9
c += 1
end
Or also:
items.each_with_index do |i, c|
puts i.to_s
break if c > 9
end
See each_with_index and also Programming Ruby Break, Redo, and Next.
Update: Chuck's answer with ranges is more Ruby-like, and nimrodm's answer using take is even better.
break works for escaping early from a loop, but it's more idiomatic just to do items[0..9].each {|i| puts i}. (And if all you're doing is literally printing the items with no changes at all, you can just do puts items[0..9].)
Another option would be
items.first(10).each do |i|
puts i.to_s
end
That reads a little more easily to me than breaking on an iterator, and first will return only as many items as available if there aren't enough.
Another variant:
puts items.first(10)
Note that this works fine with arrays of less than 10 items:
>> nums = (1..5).to_a
=> [1, 2, 3, 4, 5]
>> puts nums.first(10)
1
2
3
4
5
(One other note, a lot of people are offering some form of puts i.to_s, but in such a case, isn't .to_s redundant? puts will automatically call .to_s on a non-string to print it out, I thought. You would only need .to_s if you wanted to say puts 'A' + i.to_s or the like.)
Does this look like what you want?
10.times { |i|
puts items[i].to_s
}
items.each_with_index { |i, c| puts i and break if c <= 9 }
It was asked:
I want to grab the first 10 items then leave the "each" loop.
Use throw and catch to accomplish this, with few changes to the example:
catch(:done) do
c = 0
collected = []
items.each do |item|
collected << item
throw(:done, collected) if c == 9 # started at 0
c += 1
end
collected # if the list is less than 10 long, return what was collected
end
Simply throw the label :done with collected and the catch which is waiting for :done will return collected.
And to "ruby" this up a bit:
catch(:done) do
items.inject([]) do |collected, item|
throw(:done, collected) if collected.size == 10
collected << item # collected gets returned here and populates the first argument of this block
end
end
I do not know why some people refuse to use inject and use reduce instead (they are equivalent) when clearly the empty array given to inject([]) is being injected with items! Anyhow, the inject will return collected if there are less than 10 items.
Most answers are trying to answer what might be the intent of the question instead of what was asked and items.take(10) does make perfect sense in that case. But I can imagine wanting to grab the first items that fit within my $100 budget. Then you can simply:
catch(:done) do
items.inject({items: [], budget: 100}) do |ledger, item|
remainder = ledger[:budget] - item.price
if remainder < 0
throw(:done, ledger)
else
ledger.tap do |this|
this[:items] << item
this[:budget] = remainder
end # tap just returns what is being tapped into, in this case, ledger
end
end
end

Syntax for a for loop in ruby

How do I do this type of for loop in Ruby?
for(int i=0; i<array.length; i++) {
}
array.each do |element|
element.do_stuff
end
or
for element in array do
element.do_stuff
end
If you need index, you can use this:
array.each_with_index do |element,index|
element.do_stuff(index)
end
limit = array.length;
for counter in 0..limit
--- make some actions ---
end
the other way to do that is the following
3.times do |n|
puts n;
end
thats will print 0, 1, 2, so could be used like array iterator also
Think that variant better fit to the author's needs
I keep hitting this as a top link for google "ruby for loop", so I wanted to add a solution for loops where the step wasn't simply '1'. For these cases, you can use the 'step' method that exists on Numerics and Date objects. I think this is a close approximation for a 'for' loop.
start = Date.new(2013,06,30)
stop = Date.new(2011,06,30)
# step back in time over two years, one week at a time
start.step(stop, -7).each do |d|
puts d
end
The equivalence would be
for i in (0...array.size)
end
or
(0...array.size).each do |i|
end
or
i = 0
while i < array.size do
array[i]
i = i + 1 # where you may freely set i to any value
end
array.each_index do |i|
...
end
It's not very Rubyish, but it's the best way to do the for loop from question in Ruby
To iterate a loop a fixed number of times, try:
n.times do
#Something to be done n times
end
If you don't need to access your array, (just a simple for loop) you can use upto or each :
Upto:
2.upto(4) {|i| puts i}
2
3
4
Each:
(2..4).each {|i| puts i}
2
3
4
What? From 2010 and nobody mentioned Ruby has a fine for /in loop (it's just nobody uses it):
ar = [1,2,3,4,5,6]
for item in ar
puts item
end
['foo', 'bar', 'baz'].each_with_index {|j, i| puts "#{i} #{j}"}
Ruby's enumeration loop syntax is different:
collection.each do |item|
...
end
This reads as "a call to the 'each' method of the array object instance 'collection' that takes block with 'blockargument' as argument". The block syntax in Ruby is 'do ... end' or '{ ... }' for single line statements.
The block argument '|item|' is optional but if provided, the first argument automatically represents the looped enumerated item.

Resources