Why join won't work in this each statement
The key here is you're trying to modify i
while inside an each
block, but as i
is a local variable, it has no effect on the original.
What you want is:
arr2.collect! do |i|
i[0].upcase!
i.join
end
This rewrites the contents of the array.
What you can do, though, is roll this up into a simple gsub
:
def capitalize(string)
string.gsub(/\b([a-z])\B/) do |m|
$1.upcase
end
end
iterating over each character of a String in ruby 1.8.6 (each_char)
I have the same problem. I usually resort to String#split
:
"ABCDEFG".split("").each do |i|
puts i
end
I guess you could also implement it yourself like this:
class String
def each_char
self.split("").each { |i| yield i }
end
end
Edit: yet another alternative is String#each_byte
, available in Ruby 1.8.6, which returns the ASCII value of each char in an ASCII string:
"ABCDEFG".each_byte do |i|
puts i.chr # Fixnum#chr converts any number to the ASCII char it represents
end
Why doesn't each_slice work?
In ruby 1.8.6 you have to require 'enumerator'
(which is part of stdlib and has been merged into core in 1.8.7+) before using each_slice.
Sadly the ruby-doc lists methods that are added to core classes by stdlib without mentioning where the methods are from.
How to iterate through array of strings from database Ruby on Rails
If you're saving an "exact" array as a String, then Array#each
won't work, because isn't a method in the String class.
Maybe isn't the best option, but you could use JSON.parse and this way get your array and be able to iterate over each object inside:
require 'json'
str = '["item1", "item2", "item3"]'
JSON.parse(str).each { |item| p item }
# "item1"
# "item2"
# "item3"
In order this work your string must be an array, in your example the second item is missing its double quote.
You could consider working with serialization or array data types depending on you current database.
Ruby Loops not working properly
This isn't sane:
check_box_tag(x[i], checked_value = 1, unchecked_value = 0)
The method definition is:
check_box_tag(name, value = "1", checked = false, options = {})
You're passing 1 for value
, and 0 for checked
. You cannot "name" parameters with variable = value
, all you're doing is setting a variable and then passing the same value through to the method.
If you want to produce a checkbox where the name is the sport's name and its value is the id of the sport, get rid of your pair of loops and just us a single loop:
def sport_select(sport)
Sport.all.map do |e|
check_box_tag(e.sport_name, e.id)
end
end
If you're outputting the result to a page, you will also probably want to use .join('').html_safe
to return a single string, and prevent it from being escaped.
Related Topics
Ruby on Rails 4 - Simple_Form Multiple Select Input
Testing Routes with Subdomain Constraints Using Rspec
Ruby.Metaprogramming. Class_Eval
Why Don't More Projects Use Ruby Symbols Instead of Strings
-': Nil Can't Be Coerced into Fixnum (Typeerror)
How to Disable Wait Time for Watir
Aws Cognito User Authentication Missing Required Parameter Srp_A
Str.Each in Ruby Isn't Working
How to Click on a Specific Coordinates of an Element
Does Ruby Have Any Number Formatting Classes
How to Override [] Brackets in Ruby
Rails/Ruby Error When Creating Database: Unable to Load the Eventmachine C Extension
How to Format a Date to Mm/Dd/Yyyy in Ruby