A/An articles in Ruby [duplicate] - ruby

This question already has answers here:
Rails article helper - "a" or "an"
(6 answers)
Closed 2 years ago.
Is there something like [#pluralize in ActiveSupport][1] but for a/an articles?
So basically I need something like:
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"

It seems the Linguistics gem might be up for the job. Let's try Cary Swoveland's challenge:
require 'linguistics/en'
Linguistics.use(:en)
"one-eyed seaman".en.a
=> "a one-eyed seaman"
"honor".en.a
=> "an honor"
# And OP examples...
"urgent update".en.a
=> "an urgent update"
"status update".en.a
=> "a status update"

You could define String#articleize to return the appropriate English article:
class String
def articleize
if self[0] =~ /[aeiou]/i
"an #{self}"
else
"a #{self}"
end
end
end
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"

Related

How to use 'conflicts' with ruby trolloop

Hi I'm using trollop to parse my command line options in ruby, I have four mutually exclusive options, and one option is always required.
I'm stuck trying to figure out how to ensure only one of the four options is passed. If called with more than one option I want the usage help (educate?) shown.
I see from the trollop source there's something called conflicts
http://www.rubydoc.info/gems/trollop/2.1.2/Trollop/Parser#conflicts-instance_method
that sounds like it does what I want (?) but I can't figure out how to use it correctly.
My current stanza is effectively this
require 'trollop'
opts = Trollop::options do
opt :last, "last"
opt :first, "first"
opt :file, "filename",
:type => String
opt :date, "date to read",
:type => Date
end
Trollop::die :file, "must exist" unless File.exist?(opts[:file]) if opts[:file]
thank you
You can add the conflicts as a line in your do block like this:
require 'trollop'
opts = Trollop::options(ARGV) do
opt :last, "last"
opt :first, "first"
opt :file, "filename", :type => String
opt :date, "date to read", :type => Date
conflicts :last, :first
end
puts "Your Options Are: "
puts opts
Then you get the following output:
ruby test_options.rb --last Last --first First
Error: --last conflicts with --first.
Try --help for help.

Run ruby script and assign to var Groovy/Jenkins

Probably something simple but don't seem to be getting the output i want, but say i have a ruby script
class SlackNames
def developers
{ "Richard Lewis" => "/#richlewis",
"Name 2" => "/#name2",
"Name 3" => "/#name3"
}
end
def slack_handle
puts developers["#{ARGV[0]}"] if developers.key?("#{ARGV[0]}")
end
end
SlackNames.new.slack_handle
I would run this on the command line
ruby slack_names.rb "Richard Lewis"
Which as you can see will just return a keys given value, when i run this in a Groovy script how can i save that value to a variable?
-- Groovy
-- DEV_NAME here equals "Richard Lewis"
SLACK_NAME = sh """#!/bin/bash -l
ruby ruby_scripts/slack_names.rb \"${DEV_NAME}\"
"""
echo "${SLACK_NAME}"
When i echo out SLACK_NAME it comes back as blank
What am i doing wrong here ?
Thanks
Well first of all, you are calling the method from still within the class. (May just be a typo in the post)
Second, why ".key?("#{ARGV[0]}")" thingy? In ruby, if the hash doesn't have a value for a given key, it will just return nil and nil has the boolean value of false.
class SlackNames
def developers
{ "Richard Lewis" => "/#richlewis",
"Name 2" => "/#name2",
"Name 3" => "/#name3"
}
end
def slack_handle
developers["#{ARGV[0]}"] if developers["#{ARGV[0]}"]
end
end
puts SlackNames.new.slack_handle
This works correctly for me at least.
ruby slack_names.rb "Richard Lewis"
=> /#richlewis
I hope it helps and that I understood your problem correctly.
Just to add to CookieNinjas answer once i added this in groovy everything works as expected
SLACK_NAME = sh (
script: """#!/bin/bash -l
ruby ruby_scripts/slack_names.rb \"${DEV_NAME}\"
""",
returnStdout: true
)

Multiple case/when conditions with || [duplicate]

This question already has answers here:
Case statement with multiple values in each 'when' block
(6 answers)
Closed 6 years ago.
I've been testing this code and it's not working as I expected. Can someone shed some light on this please ?
language = { JS: "Websites", Python: "Science", Ruby: "Web apps" }
puts "What language would you like to know? "
choice = gets.chomp
case choice
when "js" || "JS"
puts "Websites!"
when "Python" || "python"
puts "Science!"
when "Ruby" || "ruby"
puts "Web apps!"
else
puts "I don't know!"
end
, When I put in the first entry it runs, but if I use the latter entry it prints "I don't Know!"
i.e : if I enter 'js' runs, but If I enter 'JS' it throws 'I don't know!'
Please do search before asking question, you can get its answer easily in other questions
choice = gets.chomp
case choice
when 'js', 'JS'
puts 'Websites!'
when 'Python', 'python'
puts 'Science!'
when 'Ruby', 'ruby'
puts 'Web apps!'
else
puts "I don't know!"
end
After suggestions
choice = gets.chomp
puts case choice
when 'js', 'JS'
'Websites!'
when 'Python', 'python'
'Science!'
when 'Ruby', 'ruby'
'Web apps!'
else
"I don't know!"
end

Why it goes into the right branch when wrong hash name is called?

I'm new in learning Ruby.In following code ,I made a mistake,writing a wrong word 'hash' instead of 'movies'.But I find it also goes into the right way .WHY this happened
?Could someone explain for me?
movies = {:Frozen => 9}
puts "input a movie title:"
title = gets.chomp
puts "the rating of the movie:"
rating = gets.chomp
if hash[title.to_i].nil? #here,i wrote 'hash' instead of 'movies'
movies[title.to_sym] = rating.to_i
puts "Added successfully!"
else
puts "The movie already exists."
end
These are my inputs and the running results:
Why hash[title.to_i] seems be no-nil and still puts out 'The movie already exists.' ?
input a movie title:
Frozen
the rating of the movie:
4
The movie already exists.
You are calling the hashmethod of main
p self # => main
p self.methods.sort #=> [:!, :!=, :! ... :hash,...]
p self.hash # => -1928951575263865998

How to parse rMeetup response

I'm using rMeetup gem that queries via api version 2 and I don't understand how to extract the "members" value from a response. Here is where I get stuck (using irb for this example):
>> require 'rmeetup'
=> true
>> client = RMeetup::Client.new do |config| config.api_key = "LALAMYKEYNOTYOURS" end
=> #<RMeetup::Client:0x007fbda4b58060 #configuration=#<RMeetup::Configuration:0x007fbda4b63fa0 #api_key="LALAMYKEYNOTYOURS">>
>> results = client.fetch(:groups, {:group_urlname => 'San-Francisco-Riak-Meetup'})
=> [#<RMeetup::Type::Group:0x007fbda4b80088 #group={"utc_offset"=>-25200000, "country"=>"US", "visibility"=>"public", "city"=>"San Francisco", "timezone"=>"US/Pacific", "created"=>1278976613000, "topics"=>[{"urlkey"=>"opensource", "name"=>"Open Source", "id"=>563}, {"urlkey"=>"web", "name"=>"Web Technology", "id"=>10209}, {"urlkey"=>"big-data", "name"=>"Big Data", "id"=>18062}, {"urlkey"=>"database-development", "name"=>"Database Development", "id"=>21506}, {"urlkey"=>"erlang-programming", "name"=>"Erlang Programming", "id"=>46514}, {"urlkey"=>"nosql", "name"=>"NoSQL", "id"=>58162}, {"urlkey"=>"riak", "name"=>"Riak", "id"=>112355}, {"urlkey"=>"distributed-systems", "name"=>"Distributed Systems", "id"=>113032}], "link"=>"http://www.meetup.com/San-Francisco-Riak-Meetup/", "rating"=>4.57, "description"=>"<p>A monthly meetup for those in the Bay Area to talk Riak, distributed systems, and app. development.</p>", "lon"=>-122.4000015258789, "group_photo"=>{"highres_link"=>"http://photos4.meetupstatic.com/photos/event/e/6/9/e/highres_16559038.jpeg", "photo_id"=>16559038, "photo_link"=>"http://photos4.meetupstatic.com/photos/event/e/6/9/e/600_16559038.jpeg", "thumb_link"=>"http://photos2.meetupstatic.com/photos/event/e/6/9/e/thumb_16559038.jpeg"}, "join_mode"=>"open", "organizer"=>{"member_id"=>140545442, "name"=>"Basho"}, "members"=>696, "name"=>"San Francisco Riak Meetup", "id"=>1674527, "state"=>"CA", "urlname"=>"San-Francisco-Riak-Meetup", "category"=>{"name"=>"tech", "id"=>34, "shortname"=>"tech"}, "lat"=>37.790000915527344, "who"=>"Riaktors"}>]
>> results.each do |k| puts k["members"] end
This is likely my misunderstanding of how to query the #group within this result. I haven't found anything that clarifies it despite similar questions on SO and other sites.
I figured this out today. This is an example of confusing a method with a hash. The proper syntax is:
results.each do |k| puts k.members end
Because members is a method of the Meetup::Type::Group, at least from the looks of it. That's not how it's documented, but it works.
The original syntax of k["members"] would work if k was a hash:
irb(main):027:0> k = {"members" => 1000}
=> {"members"=>1000}
irb(main):028:0> k["members"]
=> 1000

Resources