Add to_str method
Closed this issue · 10 comments
alias_method :to_str, :to_s
in MaterialIcon Class
Rails safe_join calls to_str on objects passed before they are concatenated
Hello @mintuhouse,
I check the code of safe_join helper:
def safe_join(array, sep=$,)
sep = ERB::Util.unwrapped_html_escape(sep)
array.flatten.map! { |i| ERB::Util.unwrapped_html_escape(i) }.join(sep).html_safe
end
In Rails 4.2, ERB::Util.unwrapped_html_escape doesn't call to to_str
method. It's only perform a gsub
in strings that not are marked as html_safe
.
In Rails master ERB::Util.unwrapped_html_escape method is different, but I can't find any reference to to_str
method.
Where do you see safe_join
is calling to_str
method?
Hi @Angelmmiguel
Sorry, I over looked. I got can't convert MaterialIcon to String (MaterialIcon#to_str gives MaterialIcon)
in this line
Just realized that this safe_join is a custom implementation from the gem
I feel its still a good thing to have as [material_icon, some_view_helpers].join
gives above error.
No problem :). What version of Ruby are you using @mintuhouse?
2.1.8
Okey.
I find this interesting article about the difference between to_s
and to_str
methods. I think MaterialIcons
meets the requirement to implement to_str
, the purpose of the class is to be represented as String
.
I will add this functionality for the next release of Material Icons gem. While next version is released, you can use material_icon.to_s
in these situations because String
object implements to_str
method.
ok. Thanks @Angelmmiguel
@mintuhouse I investigate the problem and it comes from the way to declare the icon name. I use a custom method_missing
to set all non-defined methods as the icon to display:
# Undefined method will ref to the icon.
def method_missing(name)
@icon = clear_icon(name)
self
end
Array#join
call to to_str
method that is not defined in MaterialIcon
. I think Ruby process this case and call to_s
to return a String
. MaterialIcon
returns itself and join
fails.
I think I will migrate this behaviour to an icon
method because using method_missing
is causing some problems.
But wouldn't that be a backward incompatible change (assuming we won't maintain complete list of allowed icons - in say .yml file)?
Yes, the problem is that change breaks compatibility with older versions. That's the reason I haven't changed it yet :)