Array of objects in ruby - ruby-on-rails

I am trying to learn ruby and have a doubt regarding passing arrays of objects as function parameters and printing it in the function.
I have an array that contains an array of objects as follows
describe Name
par1 = "John"
par2 = "Miley"
par3 = "Maria"
#obj_arr = [Name.new(par1),Name.new(par2),Name.new(par3)]
Name.func1(#obj_arr)
I want to print the name "John", "Miley" and "Maria" in the function and I wrote the function func1 is as follows :
def self.func1(parameter)
parameter.each do |p|
puts p
end
end
This did not print the names. Am I going wrong in accessing the obj_arr in the function?

I think your problem might be the to_s method of the object. You should override it to print what you want. BTW, the syntax in your question is a bit off. I think the definition of the function should be def self.func1 and that your missing an end.
This is the code I tested:
irb(main):001:0> class Name
irb(main):002:1> def self.func1(parameter)
irb(main):003:2> parameter.each do |p|
irb(main):004:3* puts p
irb(main):005:3> end
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> class Name
irb(main):009:1> def initialize(name)
irb(main):010:2> #name = name
irb(main):011:2> end
irb(main):012:1> end
=> nil
irb(main):013:0> Name.func1([Name.new('a'), Name.new('b')])
#<Name:0x2163dc8>
#<Name:0x2163d98>
=> [#<Name:0x2163dc8 #name="a">, #<Name:0x2163d98 #name="b">]
irb(main):014:0> class Name
irb(main):015:1> def to_s
irb(main):016:2> #name
irb(main):017:2> end
irb(main):018:1> end
=> nil
irb(main):019:0> Name.func1([Name.new('a'), Name.new('b')])
a
b
=> [a, b]
irb(main):020:0>

It may be that func1 is defined on a instance of class Name and not the class itself?
Try:
class Name
def self.func1(parameter)
parameter.each do |p|
puts p
end
end
end

Related

How to implement an object in Ruby with regex

Absolute beginner in Ruby.
I need to create a class that contains the following keys:
I know that this might be the structure, but can anybody help me with syntax?
class PixKey
def cpf
^[0-9]{11}$
end
def cnpj
^[0-9]{14}$
end
def phone
^\+[1-9][0-9]\d{1,14}$
end
def email
^[a-z0-9.!#$&'*+\/=?^_`{
end
def evp
[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}
end
end
You can define regular expressions using the /.../ regular expression literal.
Since regular expressions are immutable, I would simply use constants:
class PixKey
CPF = /^[0-9]{11}$/
CNPJ = /^[0-9]{14}$/
PHONE = /^\+[1-9][0-9]\d{1,14}$/
EMAIL = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
EVP = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
end
In the above, I've changed the email regexp to the one suggested by the HTML standard because the one in your screenshot was probably destroyed by a markdown parser.
You can use the above like this:
PixKey::CPF.match?('12345678901') #=> true
PixKey::CNPJ.match?('12345678901234') #=> true
PixKey::PHONE.match?('+5510998765432') #=> true
PixKey::EMAIL.match?('pix#bcb.gov.br') #=> true
PixKey::EVP.match?('123e4567-e89b-12d3-a456-426655440000') #=> true
Of course, you're not limited to match?, you can use any method from the Regexp class or pattern matching methods from String.
Note that in Ruby, ^ and $ match beginning and end of line which can cause problems in multi-line strings:
string = "before
+5510998765432
after"
string.match?(PixKey::PHONE) #=> true
If you want to match beginning and end of string (i.e. only match whole strings), you can use \A and \z instead:
PixKey::PHONE = /\A\+[1-9][0-9]\d{1,14}\z/
string = "before
+5510998765432
after"
string.match?(PixKey::PHONE) #=> false
string = '+5510998765432'
string.match?(PixKey::PHONE) #=> true
To return the regular expressions for further use, you could return the regex using /:
class PixKey
def cpf
/^[0-9]{11}$/
end
end
You could run PixKey.new.cpf to return the regex:
irb(main):022:0> PixKey.new.cpf
=> /^[0-9]{11}$/
You could also make it a class method by putting self. in front of the method name or add the line class << self as the first line in your class to make them all class methods by default (don't forget the end in this case).
class PixKey
def self.cpf
/^[0-9]{11}$/
end
end
class PixKey
class << self
def cpf
/^[0-9]{11}$/
end
end
end
With this you could run PixKey.cpf to return the regex:
irb(main):022:0> PixKey.cpf
=> /^[0-9]{11}$/

Collect KeyErrors from Ruby hash into array

I need to extract multiple fields from hash. But I respect my client and I want to gather all missed fields instead of returning it one by one. My idea was to use #fetch, intercept error with KeyError, put error.key into instance variable array and return proper error explanation with full list of missed keys.
Something like that
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
return 'Error'
end
private
def extract_values
{
value_1: #hash.fetch(:required_field_1),
value_2: #hash.fetch(:required_field_2),
value_3: #hash.fetch(:required_field_3)
}
end
end
When I try to process hash without required fields I got 'Error' after the first missed field:
pry(main)> Extractor.new(hash: {}).call
Field was missed
=> "Error"
Any clues?
DrySchema and other hash validators are not an option.
An issue with the provided solution is that the extracted values are never returned in the happy path (which presumably is important?). The call method is also stateful / non-idempotent. Subsequent calls to call will duplicate the missing-keys.
Finally - not sure how it's being used, but I don't love a method that returns either a hash or a string.
An alternative that attempts to follow a more functional pattern might look like:
class Extractor
attr_reader :hash, :missed_keys, :required_keys
def initialize hash
#hash = hash
#missed_keys = []
#required_keys = [:required_field_1, :required_field_2, :required_field_3]
end
def call
validate_keys_exist!
extract_values
end
private
def validate_keys_exist!
missed_keys = find_missing_keys
raise MissingKeysError, "Missed keys: #{missed_keys.join(', ')}" if missed_keys.any?
end
def find_missing_keys
required_keys - hash.keys
end
def extract_values
hash.slice(*required_keys)
# not sure if you need to map the keys to new values.
# if so you can iterate over a hash of `from: :to` pairs instead of the
# required_keys array.
end
end
Ok, I got it. The reason is in intercept level and method closures.
In aforementioned implementation Ruby tried to execute call method, got an error and exits.
If we rework it like that:
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
end
private
def extract_values
{
value_1: #hash.fetch(:required_field_1),
value_2: #hash.fetch(:required_field_2),
value_3: #hash.fetch(:required_field_3)
}
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
nil
end
end
it looks better, but still not what we wanted:
pry(main)> Extractor.new(hash: {}).call
Field was missed
=> "Missed keys: required_field_1"
This is because ruby tried to execute extract_values method, encounters first missed value and exits
So the solution as follow:
class Extractor
def initialize hash
#hash = hash
#missed_keys = []
end
def call
extract_values
return "Missed keys: #{#missed_keys.join(', ')}" if #missed_keys.present?
end
private
def extract_values
{
value_1: fetch_value(:required_field_1),
value_2: fetch_value(:required_field_2),
value_3: fetch_value(:required_field_3)
}
end
def fetch_value(key)
#hash.fetch(key)
rescue KeyError => e
puts 'Field was missed'
#missed_keys << e.key
nil
end
end
Extractor.new(hash: {}).call
Field was missed
Field was missed
Field was missed
=> "Missed keys: required_field_1, required_field_2, required_field_3"
Error interception is accomplished on the fetch_value level and Ruby skips required values one by one

Is it possible to call super to invoke alias method in a method that is being aliased

Let's say in the activerecord model car there are two boolean fields: suv and blue
If there is a method in the car model defined as such
def suv
something_true? ? super : false
end
alias :blue :suv
Now if something_true? is true, the "super" works if i invoke car.suv. However, it does not work if i invoke car.blue, then the car.blue instead returns the value of suv stored in the database. Is there anyway to make this work?
It's a clever idea but I don't think it will work. Even if it's accessed through an alias, calling super inside the method suv will only call suv. You can use metaprogramming though:
class A
def a; 1; end
def b; 2; end
end
class B < A
def initialize(condition)
#condition = condition
end
%i{a b}.each do |fn|
define_method(fn) do
#condition ? super() : "default"
end
end
end
puts B.new(false).a # => "default"
puts B.new(false).b # => "default"
puts B.new(true).a # => 1
puts B.new(true).b # => 2

Ruby loops and classes; splitting a string into an array and back to a string again

Ruby newbie here working on loops with classes. I was supposed create a method that would take a string and add exclamation points to the end of each word (by making it an array with .split) and join the 'exclaimed' words as a string again. I've been at this for two hours already and decided I should seek help. I have a handful of ideas but I keep coming up with a NoMethod error. Below is one of ways that made sense to me but of course, it doesn't work. I've also added specs at the very end.
class StringModifier
attr_accessor :string
def initialize(string)
#string = string
end
def proclaim
new_array = []
string.split.each do |word|
new array = "#{word}!"
new_array.join
end
new_array
end
end
SPECS
describe StringModifier do
describe "#proclaim" do
it "adds an exclamation mark after each word" do
blitzkrieg_bop = StringModifier.new("Hey ho let's go").proclaim
expect(blitzkrieg_bop).to eq("Hey! ho! let's! go!")
end
end
end
Write your method as:
def proclaim
string.split.map { |word| "#{word}!" }.join(" ")
end
Or write it as :
def proclaim
a = string.split
("%s! " * a.size % a).strip
end
Tested :
[30] pry(main)> a = "Hey ho let's go".split
=> ["Hey", "ho", "let's", "go"]
[31] pry(main)> ("%s! " * a.size % a).strip
=> "Hey! ho! let's! go!"
[32] pry(main)>

a method that changes the result of a setter

I am assigned to write some ruby code that will work with the following (segment of a) rspec test:
before do
#book = Book.new
end
describe 'title' do
it 'should capitalize the first letter' do
#book.title = "inferno"
#book.title.should == "Inferno"
end
This is the solution, but I don't understand it:
class Book
attr_reader :title
def title=(new_title)
words = new_title.split(" ")
words = [words[0].capitalize] +
words[1..-1].map do |word|
little_words = %w{a an and the in of}
if little_words.include? word
word
else
word.capitalize
end
end
#title = words.join(" ")
end
end
I think I am correct to deduce that #book.title = "inferno" will run the title method and eventually create a new value for the #title variable at the bottom. I know that this causes #book.title to update to "Inferno" (capitalized), but I'm not sure why. Is this a case of def title being some sort of variable method, and #title being it's final value? That's my best guess at this point.
EDIT in case it's not clear, what I'm not understanding is why setting #book.title ='inferno' causes #book.title to update to "Inferno".
When you have setter and getter methods in Ruby:
attr_writer :something
attr_reader :something
From my little understanding of this, these methods are equivalent to
def something=(value)
#something = value
end
def something
#something
end
Respectively.
Or in one statement, it could be:
attr_accessor :something
Anyway, what you are doing is to write the setter method yourself, capitalising each word of the string passed as an argument.
Your understanding is almost correct. Here is a simple example
class Chapter
attr_reader :title
def title=(new_title)
#title = new_title.reverse
end
end
#c = Chapter.new
#c.title = "ybuR"
#c.title #=> Ruby

Resources