Reputation: 852
i would play a bit with Ruby.
Now i have a little problem, which i cannot solve.
The code:
@href = "http://localhost:3000"
def link(title, href)
if href.nil?
href = @href
else
href = href
end
output = "<a href=\"#{href}\">"
output << "#{title}"
output << "</a>"
puts output
end
alias link_to link
link_to("Google","")
Whats the problem in my code? I want set a default value for the href,if it is nil.
Greets,
Peter
Upvotes: 0
Views: 87
Reputation: 27779
Here's a cleaner way to set the default:
def link(title, href = 'http://localhost:3000')
# ...
end
Upvotes: 2
Reputation: 6828
"" != nil
[10] pry(main)> link "xxx",nil
<a href="http://localhost:3000">xxx</a>
=> nil
Upvotes: 1
Reputation: 7766
The problem is here
>> "".nil?
=> false
You should use .nil? or .empty?
as the condition.
>> "".empty?
=> true
Upvotes: 1