How to write single line blocks - ruby

I find myself trying to use single line blocks but end up having to break up into multiple lines anyway. My most recent example is that I'm trying to get initials from the name field of an active record object.
#employee.name = "John Doe"
and I want to return "JD".
The only way I knew how to do it was to initialize a string, then split the name, then add to the initialized string. At the very least how can I avoid having to initialize the empty string?
def initials # In model
intials = ''
name_array = self.name.split(" ")
name_array.each { |name| initials += name[0].capitalize }
return initials
end

Let me play with some proof of concept
class Employee
attr_accessor :name
def initials
#name.split(' ').map { |name| name[0] }.join
end
end
e = Employee.new
e.name = "Foo Baz"
p e.initials # FB

I'd try things like:
'John Doe'.scan(/\b[A-Z]/).join # => "JD"
'John Doe'.scan(/\b[a-z]/i).join # => "JD"
Any of these sort of expressions can break down with names with sufficient complexity:
'John Doe-Smith'.scan(/\b[a-z]/i).join # => "JDS"
'John Doe Jr. III'.scan(/\b[a-z]/i).join # => "JDJI"
'Dr. John Doe MD'.scan(/\b[a-z]/i).join # => "DJDM"
Then it becomes a case of needing to strip out the parts that you don't want.

Related

How to grep for a pattern in a file and store the content following it?

My file content is
blablabla
Name : 'XYZ'
Age : '30'
Place : 'ABCD'
blablabla
How can I grep for "Name", "Age", "Place" and store name "XYZ", age "30" and place "ABCD" in a hash?
What should be the '?' in this code to get those?
data = {}
name = /Name/
age = /Age/
place = /Place/
read_lines(file) { |l|
case l
when name
data[:name] = ?
when age
data[:age] = ?
when place
data[:place]= ?
end
}
You can use something like this.
data = {}
keys = {:name => "Name", :age => "Age", :place => "Place"}
File.open("test.txt", "r") do |f|
f.each_line do |line|
line.chomp!
keys.each do |hash_key, string|
if line[/#{string}/]
data[hash_key] = line.strip.split(" : ")[-1].gsub("'", "")
break
end
end
end
end
output
p data
# => {:name=>"XYZ", :age=>"30", :place=>"ABCD"}
Strange code, but in this case:
data[:name] = l.split(':')[1] if l.match(name)
when age
data[:age] = l.split(':')[1] if l.match(age)
when place
data[:place]= l.split(':')[1] if l.match(place)
Are you interested in refactoring?
One option is to:
mapping =
[
{ name: :name, pattern: /Name/ },
{ name: :age, pattern: /Age/ },
{ name: :place, pattern: /Place/ }
]
data = str.split(/\r?\n|\r/).map do |line|
mapping.map{|pair|
{ pair[:name] => line.split(' : ')[1].gsub("'", "") } if line.match(pair[:pattern])
}.compact.reduce({}, :merge)
end.reduce({}, :merge)
Suppose we first read the file into a string:
str = File.read('fname')
which is:
str =<<_
blablabla
Name : 'XYZ'
Age : '30'
Place : 'ABCD'
blablabla
_
#=> "blablabla\nName : 'XYZ'\nAge : '30'\nPlace : 'ABCD'\nblablabla\n"
Then use the regex
r = /
^ # match beginning of line
Name\s*:\s*'(.*)'\n # match 'Name`, ':' possibly surrounded by spaces, any number
# of any character in capture group 1, end of line
Age\s*:\s*'(.*)'\n # match 'Age`, ':' possibly surrounded by spaces, any number
# of any character in capture group 2, end of line
Place\s*:\s*'(.*)'\n # match 'Place`, ':' possibly surrounded by spaces, any number
# of any character in capture group 3, end of line
/x # free-spacing regex definition mode
with String#scan to form the hash:
[:name, :age, :place].zip(str.scan(r).first).to_h
#=> {:name=>"XYZ", :age=>"30", :place=>"ABCD"}
I'd do something like this:
str = <<EOT
blablabla
Name : 'XYZ'
Age : '30'
Place : 'ABCD'
blablabla
EOT
str.scan(/(Name|Age|Place)\s+:\s'([^']+)/).to_h # => {"Name"=>"XYZ", "Age"=>"30", "Place"=>"ABCD"}
scan will create sub-arrays if it sees pattern groups in the regular expression. Those make it easy to turn the returned array of arrays into a hash.
If you need to fold the keys to lower-case, or convert them to symbols:
str.scan(/(Name|Age|Place)\s+:\s'([^']+)/)
.map{ |k, v| [k.downcase, v] } # => [["name", "XYZ"], ["age", "30"], ["place", "ABCD"]]
.to_h # => {"name"=>"XYZ", "age"=>"30", "place"=>"ABCD"}
Or:
str.scan(/(Name|Age|Place)\s+:\s'([^']+)/)
.map{ |k, v| [k.downcase.to_sym, v] } # => [[:name, "XYZ"], [:age, "30"], [:place, "ABCD"]]
.to_h # => {:name=>"XYZ", :age=>"30", :place=>"ABCD"}
Or some variation on:
str.scan(/(Name|Age|Place)\s+:\s'([^']+)/)
.each_with_object({}){ |(k,v), h| h[k.downcase.to_sym] = v}
# => {:name=>"XYZ", :age=>"30", :place=>"ABCD"}
If the example string truly is the complete file, and there won't be any other reoccurrence of the key/value pairs, then this will work. If there could be more than one then the resulting hash will not be correct because the subsequent pairs will stomp on the first one. If the file is as you said, then it'll work fine.

How do I calling an object's method using strings?

I've been looking for an answer to this question for a while, but I haven't been able to find one that I was able to understand and apply.
I have a class that contains three instance variables: #brand, #setup, and #year. I have a module, included in that class, that has three methods: print_brand(), print_setup(), and print_year() that simply print the value assigned to the associated variable.
I'd like to get two strings from a user and use the first as an object name and the second as a method name. Here's what I have right now:
class Bike
include(Printers)
def initialize(name, options = {})
#name = name
#brand = options[:brand]
#setup = options[:setup]
#year = options[:year]
end
end
trance = Bike.new("trance x3", {
:brand => "giant",
:setup => "full sus",
:year => 2011
}
)
giro = Bike.new("giro", {
:brand => "bianchi",
:setup => "road",
:year => 2006
}
)
b2 = Bike.new("b2", {
:brand => "felt",
:setup => "tri",
:year => 2009
}
)
puts "Which bike do you want information on?"
b = gets()
b.chomp!
puts "What information are you looking for?"
i = gets()
i.chomp!
b.send(i)
I'm missing some functionality that converts b from a string to an object name. For example, I want the user to be able to type in "trance" and then "print_year" and have "2011" printed on the screen. I tried to use constantize on b, but that doesn't seem to work. I get the error:
in 'const_defined?': wrong constant name trance (NameError)
Any other ideas?
You should store the object in a hashmap with key = name and value = object, then use b (name) to retrieve the right object from the hashmap. I'm still not sure what do you want to do with the second input, by my guess is that this answer covers that as well.
h = Hash.new()
h["trance x3"] = trance
h["giro"] = giro
...
puts "Which bike do you want information on?"
b = gets()
b.chomp!
user_bike = h[b]
puts "What information are you looking for?"
i = gets()
i.chomp!
user_bike.send(i)
I would use eval:
eval "#{ b }.#{ i }"
I suppose you have to add accessors:
attr_accessor :brand, :setup, :year

What's the best way to handle more than 100 conditions in Ruby?

How can I handle a large number of conditions in a case statement?
...I'm about to write a case statement with about 125 when's.
This is along the lines of what I'm doing now, based on each when I add a node to a Nokogiri XML document, each when has two values that get set in the node, before setting the namespace:
case var
when :string
property_uom_node = Nokogiri::XML::Node.new "test_value", #ixml.doc
property_uom_node['att'] = "val"
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{|ns| ns.prefix=="dt"}
property_uom_node
when :integer
#do something else
when :blue
...
#100 more when statements
...
end
I'm not looking for domain specific advice, just if there is a clean way to do this without ending up with a 300 line method.
This is what I ended up doing:
lookup = {:lup => ["1","2"], :wup => ["1","2"]}
case param
when lookup.has_key?(param)
property_uom_node = Nokogiri::XML::Node.new "#{lookup[param][0]}", #ixml.doc
property_uom_node['att'] = #{lookup[param][1]}
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{|ns| ns.prefix=="dt"}
property_uom_node
end
Many case statements can, and many should, be replaced with other structures. Basically, the idea is to separate the policy -- what you want the code to do -- from the implementation -- how the code does it.
Suppose that your case statement is keyed on a symbol (that is, each of then when clauses is a constant symbol):
case foo
when :one
puts 1
when :two
puts 2
when :three
puts 3
else
puts 'more'
end
This can be replaced mostly with a data structure:
INTS = {:one => 1, :two => 2}
key = :one
puts INTS[key] # => 1
What if there are two different values, and not just one? Then make each value its own hash:
DOGS = {
:dog1 => {:name => 'Fido', :color => 'white},
:dog2 => {:name => 'Spot', :color => 'black spots'},
}
key = :dog2
dog = DOGS[key]
puts "#{dog[:name]}'s color is #{dog[:color]}"
# => "Spot's color is black spots"
It looks like the second case statement only has one case. A hash is a good way to do a lookup(many cases). You might try it like this:
if val = lookup[param]
property_uom_node = Nokogiri::XML::Node.new(val[0], #ixml.doc)
property_uom_node['att'] = val[1]
property_uom_node.namespace = #ixml.doc.root.namespace_definitions.find{ |ns| ns.prefix == "dt" }
property_uom_node # return the node
else
# not one of our cases
end

Merge Ruby arrays

I have a few arrays of Ruby objects of class UserInfo:
class UserInfo
attr_accessor :name, :title, :age
end
How can I merge these arrays into one array? A user is identified by its name, so I want no duplicate names. If name, title, age, etc. are equal I'd like to have 1 entry in the new array. If names are the same, but any of the other details differ I probably want those 2 users in a different array to manually fix the errors.
Thanks in advance
Redefine equality comparison on your object, and you can get rid of actual duplicates quickly with Array#uniq
class UserInfo
attr_accessor :name, :title, :age
def == other
name==other.name and title==other.title and age==other.age
end
end
# assuming a and b are arrays of UserInfo objects
c = a | b
# c will only contain one of each UserInfo
Then you can sort by name and look for name-only duplicates
d = c.sort{ |p,q| p.name <=> q.name } #sort by name
name = ""
e = []
d.each do |item|
if item.name == name
e[-1] = [e[-1],item].flatten
else
e << item
end
end
A year ago I monkey patched a kind of cryptic instance_variables_compare on Object. I guess you could use that.
class Object
def instance_variables_compare(o)
Hash[*self.instance_variables.map {|v|
self.instance_variable_get(v)!=o.instance_variable_get(v) ?
[v,o.instance_variable_get(v)] : []}.flatten]
end
end
A cheesy example
require 'Date'
class Cheese
attr_accessor :name, :weight, :expire_date
def initialize(name, weight, expire_date)
#name, #weight, #expire_date = name, weight, expire_date
end
end
stilton=Cheese.new('Stilton', 250, Date.parse("2010-12-02"))
gorgonzola=Cheese.new('Gorgonzola', 250, Date.parse("2010-12-17"))
irb is my weapon of choice
>> stilton.instance_variables_compare(gorgonzola)
=> {"#name"=>"Gorgonzola", "#expire_date"=>#<Date: 4910305/2,0,2299161>}
>> gorgonzola.instance_variables_compare(stilton)
=> {"#name"=>"Stilton", "#expire_date"=>#<Date: 4910275/2,0,2299161>}
>> stilton.expire_date=gorgonzola.expire_date
=> #<Date: 4910305/2,0,2299161>
>> stilton.instance_variables_compare(gorgonzola)
=> {"#name"=>"Gorgonzola"}
>> stilton.instance_variables_compare(stilton)
=> {}
As you can see the instance_variables_compare returns an empty Hash if the two objects has the same content.
An array of cheese
stilton2=Cheese.new('Stilton', 210, Date.parse("2010-12-02"))
gorgonzola2=Cheese.new('Gorgonzola', 250, Date.parse("2010-12-17"))
arr=[]<<stilton<<stilton2<<gorgonzola<<gorgonzola2
One hash without problems and one with
h={}
problems=Hash.new([])
arr.each {|c|
if h.has_key?(c.name)
if problems.has_key?(c.name)
problems[c.name]=problems[c.name]<<c
elsif h[c.name].instance_variables_compare(c) != {}
problems[c.name]=problems[c.name]<<c<<h[c.name]
h.delete(c.name)
end
else
h[c.name]=c
end
}
Now the Hash h contains the objects without merging problems and the problems hash contains those that has instance variables that differs.
>> h
=> {"Gorgonzola"=>#<Cheese:0xb375e8 #name="Gorgonzola", #weight=250, #expire_date=#<Date: 2010-12-17 (4911095/2,0,2299161)>>}
>> problems
=> {"Stilton"=>[#<Cheese:0xf54c30 #name="Stilton", #weight=210, #expire_date=#<Date: 2010-12-02 (4911065/2,0,2299161)>>, #<Cheese:0xfdeca8 #name="Stilton", #weight=250,#expire_date=#<Date: 2010-12-02 (4911065/2,0,2299161)>>]}
As far as I can see you will not have to modify this code at all to support an array of UserInfo objects.
It would most probably be much faster to compare the properties directly or with a override of ==. This is how you override ==
def ==(other)
return self.weight == other.weight && self.expire_date == other.expire_date
end
and the loop changes into this
arr.each {|c|
if h.has_key?(c.name)
if problems.has_key?(c.name)
problems[c.name]=problems[c.name]<<c
elsif h[c.name] != c
problems[c.name]=problems[c.name]<<c<<h[c.name]
h.delete(c.name)
end
else
h[c.name]=c
end
}
Finally you might want to convert the Hash back to an Array
result = h.values
Here's another potential way. If you have a way of identifying each UserInfo, say a to_str method that prints out the values:
def to_str()
return "#{#name}:#{#title}:#{#age}"
end
You can use inject and a hash
all_users = a + b # collection of users to "merge"
res = all_users.inject({})do |h,v|
h[v.to_str] = v #save the value indexed on the string output
h # return h for the next iteration
end
merged = res.values #the unique users

Ruby 1.9: turn these 4 arrays into hash of key/value pairs

I have four arrays that are coming in from the client. Let's say that there is an array of names, birth dates, favorite color and location. The idea is I want a hash later where each name will have a hash with respective attributes:
Example date coming from the client:
[name0, name1, name2, name3]
[loc0, loc1]
[favcololor0, favcolor1]
[bd0, bd1, bd2, bd3, bd4, bd5]
Output I'd like to achieve:
name0 => { location => loc0, favcolor => favcolor0, bd => bd0 }
name1 => { location => loc1, favcolor => favcolor1, bd => bd1 }
name2 => { location => nil, favcolor => nil, bd => bd2 }
name3 => { location => nil, favcolor => nil, bd => bd3 }
I want to have an array at the end of the day where I can iterate and work on each particular person hash.
There need not be an equivalent number of values in each array. Meaning, names are required.. and I might receive 5 of them, but I only might receive 3 birth dates, 2 favorite colors and 1 location. Every missing value will result in a nil.
How does one make that kind of data structure with Ruby 1.9?
I would probably do it like this
# assuming names, fav_colors, birth_dates, and locations are your arrays
name_collection = {}
names.zip(birth_dates, fav_colors, locations) do |name, birth_date, fav_color, location|
name_collection[name] = { :birth_date => birth_date,
:fav_color => fav_color,
:location => location }
end
# usage
puts name_collection['jack'][:fav_color] # => 'blue'
A small class to represent a person
class Person
attr_accessor :name, :color, :loc, :bd
def initialize(args = {})
#name = args[:name]
#color = args[:color]
#loc = args[:loc]
#bd = args[:bd]
end
def pp()
puts "*********"
puts "Name: #{#name}"
puts "Location: #{#loc}"
puts "Birthday: #{#bd}"
puts "Fav. Color: #{#color}"
puts "*********"
end
end
another to represent people, which is mainly just a listing of Persons.
class People
attr_accessor :list_of_people
def initialize()
#list_of_people = {}
end
def load_people(names, locations, favcolors, birthdates)
names.each_with_index do |name, index|
#list_of_people[name] = Person.new(:name => name, :color => favcolors[index], :loc => locations[index], :bd => birthdates[index])
end
end
def pp()
#list_of_people.each_pair do |key, value|
value.pp()
end
end
end
I threw in a pretty print function for each so you can see their data. With a starting point like this it will be really easy to modify and add methods that do all sorts of useful things.
if __FILE__ == $0
names = ["name0", "name1", "name2", "name3"]
locs = ["loc0","loc1"]
favcolors = ["favcolor0", "favcolor1"]
bds = ["bd0","bd1","bd2","bd3","bd4"]
a = People.new()
a.load_people(names,locs,favcolors,bds)
a.pp()
end
I think the kind of data structure you're looking for is -ahem- a Struct.
# setup data
names = %w(name0 name1 name2 name3)
locations = %w(loc0 loc1)
colors = %w(favcololor0 favcolor1)
bd = %w(bd0 bd1 bd2 bd3 bd4 bd5)
# let's go
Person = Struct.new( :name, :location, :fav_color, :bd )
all_persons = names.zip( locations, colors, bd ).map{|p| Person.new( *p)}
# done
puts all_persons
someone= all_persons.find{|p| p.name == "name1"}
puts someone.location unless someone.nil?

Resources