04-06-2019, 04:24 AM
I have found these interesting pieces of code that work on Ruby 2.6 but not before.
WARNING!
These will not help any scripting impaired person at all!
You already know the case statement, but you sure ignored the new feature found in Ruby 2.6, the endless range! You no longer need to worry about the range's last value.
There the then method works like the yield_self one and lets you chain them to the gsub or global substitution method in a readable way.
In this case chain allows people to sum up all of the ranges and easily turn them in to a single array if you wish.
Now you can merge multiple Hashes!
They work like the | and & binary operators respectively but accept multiple arguments.
Now you will know whether or not that method was defined right in Array class, if the second argument is non existing or it's true, it will tell you it belongs to Array even if it was defined in Enumerable. Of course, this will work in any other Ruby class as well.
This to_h method will allow you to stop using the Array#map{ block }.to_h call following the DRY principle or Don't Repeat Yourself!
Constant names may start with a non-ASCII capital letter so in Ruby 2.6 you can define one like ÑU = 255 (where Ñ should be pronounced like the Italian gn sound).
WARNING!
These will not help any scripting impaired person at all!
Code:
case n
when 1..9 then 'Single digit'
when 10..99 then 'Two digit'
when 100.. then 'Three or more'
end
You already know the case statement, but you sure ignored the new feature found in Ruby 2.6, the endless range! You no longer need to worry about the range's last value.
Code:
isbn = '978-1-93778-549-9'
isbn.gsub('-', '')
.then { |isbn| URI("#{API_URL}?q=isbn:#{isbn}") }
.then { |uri| Net:HTTP.get(uri) }
.then { |json_response| JSON.parse(json_response) }
.then { |response| response.dig('items', 'volumeInfo') }
There the then method works like the yield_self one and lets you chain them to the gsub or global substitution method in a readable way.
Code:
(1..3).chain((5..7), [9, 10]).to_a
#=> [1, 2, 3, 5, 6, 7, 9, 10]
list = (1..3).each + (5..7) + (9..10)
list.to_a
#=> [1, 2, 3, 5, 6, 7, 9, 10]
In this case chain allows people to sum up all of the ranges and easily turn them in to a single array if you wish.
Code:
foo = { a: 1 }
bar = { b: 2 }
baz = { c: 3 }
foo.merge(bar, baz) #=> { a: 1, b: 2, c: 3 }
# In Ruby 2.5
# foo.merge(bar).merge(baz)
Now you can merge multiple Hashes!
Code:
a = [1, 1, 2, 3]
b = [3, 4]
a.union(b) #=> [1, 2, 3, 4]
a.difference(b) #=> [1, 1, 2]
They work like the | and & binary operators respectively but accept multiple arguments.
Code:
Array.method_defined?(:to_h, false)
Now you will know whether or not that method was defined right in Array class, if the second argument is non existing or it's true, it will tell you it belongs to Array even if it was defined in Enumerable. Of course, this will work in any other Ruby class as well.
Code:
{a: 1, b: 2, c: 3}.to_h { |k, v| [k.to_s, -v] }
#=> {'a' => -1, 'b' => -2, 'c' => -3}
This to_h method will allow you to stop using the Array#map{ block }.to_h call following the DRY principle or Don't Repeat Yourself!
Constant names may start with a non-ASCII capital letter so in Ruby 2.6 you can define one like ÑU = 255 (where Ñ should be pronounced like the Italian gn sound).
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE