Unexpected Output in Ruby on Rails

Unexpected output when using `select`

p numbers.select do |x| x.even? end

is called in this way

p(numbers.select) do |x| x.even? end

The block is passed to p, not to select as you'd expect, p just ignores it

In the second case this doesn't happen because the block with {} has higher precedence than the method call. Instead, block with do-end has lower precedence than the method call.

The second case looks like this instead

p(numbers.select { |x| x.even? })

Unexpected output in Ruby on Rails

You need to remove = in this line <%= @project.tasks.each do |task| %>

<ul>
<% @project.tasks.each do |task| %>
<li>
<%= check_box_tag "task_done_#{task.id}", "", task.done %>
<%# <%= link_to task.title, task %>
<%= task.title %>
</li>
<% end %>
</ul>

Small Note:

<% %> - Executes the code.

<%= %> - Prints the Output.

Unexpected output in rails when using scopes

As per the rails api :

Scope Adds a class method for retrieving and querying objects. The method is intended to return an ActiveRecord::Relation object, which is composable with other scopes. If it returns nil or false, an all scope is returned instead.

So this is not unexpected, rather a exact expected behavior. your scope returns false, it applies all scope and dumps all of the Appliance records, which are activerecord relation objects, as stated in api.

Unexpected array output on page

<% %> Executes the Ruby code inside

<%= %> Prints the results

You are displaying array and then it's values, so you would want to change <%= %> to <% %> .

<%= @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>

to

<% @fields.select{|field| field.model=="PreferredOffering"}.each do |field| %>

ruby object to_s gives unexpected output

Just use .inspect method instead of .to_s if you want to see internal properties of objects.

Unexpected results when comparing classes in case statement vs an if statment

The triple equals === is called under the hood in a case expression in Ruby. We've found it convenient to be able to express something like:

case object
when String
"object is an instance of String!"
when Enumerable
"wow, object is actually a bunch of objects!"
end

This is convenient, of course, unless you're actually holding the class, not an instance of the class. But in your example, you call #class on the object in your case expression. Simply drop that call, and your cases will be put in the correct bins. :)

Rails console stops to output any text

This usually happens because of dangling syntax. In your case you're using both do ... end and { ... }, notably omitting the end.

The IRB parser will continue to take input until you put in an end, and only then will it run and/or emit an error.

The fix is to scratch out the do.

You want either:

Card.all.each do |c|
...
end

Or the inline style:

Card.all.each { |c| ... }


Related Topics



Leave a reply



Submit