What is the common way to handle optional method params? - ruby

I have a method in which i want to pass in dynamic params. The method is called in a loop and sometimes value2 is available and sometimes not.
What is the common way to handle optional method params?
my_method(:value1 => 1,
:value2 => 2 if foo, # this is not working
:value3 => 3)

I usually create a hash like this:
opts = {:value1 => 1,
:value3 => 3}
opts[:value2] = 2 if foo
my_method(opts)
The benefit of this approach is that everyone catches the if foo as it is a special case. Otherwise many programmers, like myself, will miss this at first glance and get confused why :value2 is not set.
Sometimes you have default settings, then you can use this approach:
default = {:value1 => 0,
:value2 => 0,
:value3 => 0}
opts = {:value1 => 1,
:value3 => 3}
my_method(default.merge(opts))
Or even better:
DEFAULT_OPTS = {:value1 => 0,
:value2 => 0,
:value3 => 0}
def my_method(opts)
opts = DEFAULT_OPTS.merge(opts)
# ...
end
my_method(...)

Related

Ruby inject condition

I have an array filled with hashes. The data structure looks like this:
students = [
{
"first_name" => "James",
"last_name" => "Sullivan",
"age" => 20,
"study_results" => {"CAR" => 1, "PR1" => 1, "MA1" => 1, "BEN" => 2, "SDP" => nil}
}
]
I want to find students with the mark 1 from at least two subjects.
I tried to convert the hash with marks into an array and then use the inject method to count the number of 1 and find out if the number is > 1:
students.select{|student
(
(student["study_results"].to_a)
.inject(0){|sum, x| sum += 1 if x.include?(1)}
) > 1
}
Is there any way to put a condition into the method, or should I find a different way to solve it?
I commend you for attempting to solve this before posting, but you made it unnecessarily complicated. I'd write it like this:
students.select{|student| student['study_results'].values.count(1) >= 2}
That's all, no need for inject. You were misusing it here.
Ruby collections have TONS of useful methods. If you find yourself using inject or each, there's a better method for this, 90% of the time.
Explanation:
student['study_results'] # => {"CAR"=>1, "PR1"=>1, "MA1"=>1, "BEN"=>2, "SDP"=>nil}
student['study_results'].values # => [1, 1, 1, 2, nil]
student['study_results'].values.count(1) # => 3
student['study_results'].values.count(2) # => 1
student['study_results'].values.count(3) # => 0
student['study_results'].values.count(nil) # => 1
Documentation
Hash#values
Array#count

`Hash()` when creating a hash

I see many seemingly interchangeable ways to create a hash. The following all create the same hash:
w = {:one => 1, :two => 2}
x = Hash[:one => 1, :two => 2]
y = Hash.[](:one => 1, :two => 2)
z = Hash.send(:[], :one => 1, :two => 2)
huh = Hash(:one => 1, :two => 2)
As for Hash(:one => 1, :two => 2), I expect to find a :() method for Hash in the documentation. Along with the documented method ::[], shouldn't the documentation also list a ::() method?
If they are both just syntactic sugar, where is the latter method documented?
It's a method in Kernel (which contains other methods that you can call directly like Kernel.puts) - Kernel.Hash. Don't use it (it's not idiomatic).

How to add new item to hash

I don't know how to add new item to already existing hash. For example, first I construct hash:
hash = {item1: 1}
After that, I want to add item2, so after this I have hash like this:
{item1: 1, item2: 2}
I don't know what method to do on hash. Could someone help me?
Create the hash:
hash = {:item1 => 1}
Add a new item to it:
hash[:item2] = 2
If you want to add new items from another hash - use merge method:
hash = {:item1 => 1}
another_hash = {:item2 => 2, :item3 => 3}
hash.merge(another_hash) # {:item1=>1, :item2=>2, :item3=>3}
In your specific case it could be:
hash = {:item1 => 1}
hash.merge({:item2 => 2}) # {:item1=>1, :item2=>2}
but it's not wise to use it when you should to add just one element more.
Pay attention that merge will replace the values with the existing keys:
hash = {:item1 => 1}
hash.merge({:item1 => 2}) # {:item1=>2}
exactly like hash[:item1] = 2
Also you should pay attention that merge method (of course) doesn't effect the original value of hash variable - it returns a new merged hash. If you want to replace the value of the hash variable then use merge! instead:
hash = {:item1 => 1}
hash.merge!({:item2 => 2})
# now hash == {:item1=>1, :item2=>2}
hash.store(key, value) - Stores a key-value pair in hash.
Example:
hash #=> {"a"=>9, "b"=>200, "c"=>4}
hash.store("d", 42) #=> 42
hash #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}
Documentation
It's as simple as:
irb(main):001:0> hash = {:item1 => 1}
=> {:item1=>1}
irb(main):002:0> hash[:item2] = 2
=> 2
irb(main):003:0> hash
=> {:item1=>1, :item2=>2}
hash[key]=value
Associates the value given by value with the key given by key.
hash[:newKey] = "newValue"
From Ruby documentation:
http://www.tutorialspoint.com/ruby/ruby_hashes.htm
hash_items = {:item => 1}
puts hash_items
#hash_items will give you {:item => 1}
hash_items.merge!({:item => 2})
puts hash_items
#hash_items will give you {:item => 1, :item => 2}
hash_items.merge({:item => 2})
puts hash_items
#hash_items will give you {:item => 1, :item => 2}, but the original variable will be the same old one.
Create hash as:
h = Hash.new
=> {}
Now insert into hash as:
h = Hash["one" => 1]

Testing hash contents using RSpec

I have a test like so:
it "should not indicate backwards jumps if the checker position is not a king" do
board = Board.new
game_board = board.create_test_board
board.add_checker(game_board, :red, 3, 3)
x_coord = 3
y_coord = 3
jump_locations = {}
jump_locations["upper_left"] = true
jump_locations["upper_right"] = false
jump_locations["lower_left"] = false
jump_locations["lower_right"] = true
adjusted_jump_locations = #bs.adjust_jump_locations_if_not_king(game_board, x_coord, y_coord, jump_locations)
adjusted_jump_locations["upper_left"].should == true
adjusted_jump_locations["upper_right"].should == false
adjusted_jump_locations["lower_left"].should == false
adjusted_jump_locations["lower_right"].should == false
end
which, I know, is verbose. Is there a more concise way to state my expectations? I've looked at the docs but I can't see where to compress my expectations. Thanks.
It works for hashes too:
expect(jump_locations).to include(
"upper_left" => true,
"upper_right" => false,
"lower_left" => false,
"lower_right" => true
)
Source:
include matcher # relishapp.com
Just wanna add to #David's answer. You could nest and use matchers in your include hash. For example:
# Pass
expect({
"num" => 5,
"a" => {
"b" => [3, 4, 5]
}
}).to include({
"num" => a_value_between(3, 10),
"a" => {
"b" => be_an(Array)
}
})
A caveat: a nested include hash must test all keys or the test will fail, e.g.:
# Fail
expect({
"a" => {
"b" => 1,
"c" => 2
}
}).to include({
"a" => {
"b" => 1
}
})
Syntax has changed for RSpec 3, but include matcher is still the one:
expect(jump_locations).to include(
"upper_left" => true,
"upper_right" => false,
"lower_left" => false,
"lower_right" => true
)
See built-in-matchers#include-matcher.
Another easy way to test if the whole content is a Hash is to checkout if the content is the Hash Object itself:
it 'is to be a Hash Object' do
workbook = {name: 'A', address: 'La'}
expect(workbook.is_a?(Hash)).to be_truthy
end
For the question above we can check as follow:
expect(adjusted_jump_locations).to match(hash_including('upper_left' => true))

Ruby equivalent of Perl Data::Dumper

I am learning Ruby & Perl has this very convenient module called Data::Dumper, which allows you to recursively analyze a data structure (like hash) & allow you to print it. This is very useful while debugging. Is there some thing similar for Ruby?
Look into pp
example:
require 'pp'
x = { :a => [1,2,3, {:foo => bar}]}
pp x
there is also the inspect method which also works quite nicely
x = { :a => [1,2,3, {:foo => bar}]}
puts x.inspect
I normally use a YAML dump if I need to quickly check something.
In irb the syntax is simply y obj_to_inspect. In a normal Ruby app, you may need to add a require 'YAML' to the file, not sure.
Here is an example in irb:
>> my_hash = {:array => [0,2,5,6], :sub_hash => {:a => 1, :b => 2}, :visible => true}
=> {:sub_hash=>{:b=>2, :a=>1}, :visible=>true, :array=>[0, 2, 5, 6]}
>> y my_hash # <----- THE IMPORTANT LINE
---
:sub_hash:
:b: 2
:a: 1
:visible: true
:array:
- 0
- 2
- 5
- 6
=> nil
>>
The final => nil just means the method didn't return anything. It has nothing to do with your data structure.
you can use Marshal, amarshal, YAML

Resources