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
Ruby on Rails Scalability/Performance
Ruby/Rails: Converting a Date to a Unix Timestamp
File.Expand_Path("../../Gemfile", _File_) How Does This Work? Where Is the File
Docker for MAC - Mkmf.Rb Can't Find Header Files for Ruby
Understanding the Gemfile.Lock File
How to Sort a Ruby Hash by Number Value
How to Get Sinatra to Auto-Reload the File After Each Change
Difference Between Has_One and Belongs_To in Rails
Any Success with Sinatra Working Together with Eventmachine Websockets
How to Pass Parameter on 'Vagrant Up' and Have It in the Scope of Vagrantfile
Ruby: How to Iterate Over a Range, But in Set Increments
How to Return Early from a Rake Task
"Which in Ruby": Checking If Program Exists in $Path from Ruby
How to Merge Two Hashes Without Overwritten Duplicate Keys in Ruby