How to print unicode character U-1F4A9 'pile of poo' emoji
Unicode code points with more than four hex digits must be enclosed in curly braces:
puts "\u{1f4a9}"
# => br>
This is pretty poorly documented, so don't feel bad about not figuring it out. A nice thing about the curly brace syntax is that you can embed multiple code points separated by spaces:
puts "\u{1f4a9 1f60e}"
# => br>
Of course, since Ruby 2.0, UTF-8 has been the default encoding, so you can always just put the emoji directly into your source:
puts "br># => br>
How to visualize my ruby program which simulates the growth of a tree?
I ended up using a combination of iGian and anquegi's answers (THANK YOU!).
You can try the game out at: https://repl.it/@benisburgers/OrangeTree
class OrangeTree
def initialize
@age = 0
@orangeCount = 0
@height = 0
puts "Welcome to the orange-tree game. You can leave the game at any time by typing 'exit'. "
puts "Would you like to plant an orange tree?"
getFirstInput
end
def getFirstInput
@firstInput = gets.downcase.chomp
if @firstInput == 'yes'
drawTree
puts @tree
puts "Congratulations. You have planted an orange tree"
puts "There are still no oranges. Would you like to wait a few more years?"
waitFewYears?
elsif @firstInput == 'no'
puts "Goodbye"
exit
elsif @firstInput == "exit"
puts "Goodbye"
exit
else
puts "Please type 'yes', 'no', or 'exit'"
getFirstInput
end
end
def waitFewYears?
@input = gets.downcase.chomp
if @input == 'yes'
puts "Here you go"
@age = 4
orangeGrowth
countTheOranges
elsif @input == 'no'
puts "Gooybye"
exit
elsif @input == 'exit'
puts "Goodbye"
exit
else
puts "Please type 'yes', 'no', or 'exit'"
waitFewYears?
end
end
def waitOneYear?
puts "Do you want to wait another year?"
@wantsToWait = gets.downcase.chomp
if @wantsToWait == "yes"
puts
oneYearPasses
elsif @wantsToWait == "no"
puts "Goodbye"
exit
elsif @wantsToWait == "exit"
puts "Goodbye"
exit
else
puts "Please type 'yes', 'no', or 'exit'"
waitOneYear?
end
end
def oneYearPasses
@orangeCount = 0
@tree.replace(@originalTree)
puts "Another year has passed"
@age = @age + 1
puts "The tree is #{@age.to_s} years old"
@height = @height + 1
orangeGrowth
countTheOranges
pickAnOrange?
end
def orangeGrowth
if @age < 20
@orangeCount = @orangeCount + @age - 3
else
@orangeCount = 17
end
i = 0
while i < @orangeCount
@tree["_"] = "br> i = i + 1
end
puts @tree
end
def countTheOranges
if @orangeCount < 1
puts "There are still no oranges"
elsif @orangeCount == 1
puts "There is one orange on the tree"
pickAnOrange?
else
puts "There are #{@orangeCount} oranges on the tree"
pickAnOrange?
end
end
def pickAnOrange?
puts "Would you like to pick an orange?"
@wantsApple = gets.downcase.chomp
if @orangeCount > 0
if @wantsApple == "yes"
@orangeCount = @orangeCount - 1
@tree["] = "_"
puts @tree
puts "That was delicious"
if @orangeCount < 1
puts "There are no more oranges left"
elsif @orangeCount == 1
puts "There is one more orange left"
pickAnOrange?
else
puts "There are #{@orangeCount} oranges left"
pickAnOrange?
end
elsif @wantsApple == "no"
puts "Alright, hombre"
elsif @wantsApple == "exit"
puts "Goodbye"
exit
else
puts "Please type 'yes', 'no', or 'exit'"
pickAnOrange?
end
end
waitOneYear?
end
def drawTree
@tree = <<-'EOF'
\/ | |/
\/ / \||/ /_/___/_
\/ |/ \/
_\_\_\ | /_____/_
\ | / /
__ _-----` |{,-----------~
\ }{
}{{
}}{
{{}
, -=-~{ .-^- _
ejm `}
{
EOF
@originalTree = <<-'EOF'
\/ | |/
\/ / \||/ /_/___/_
\/ |/ \/
_\_\_\ | /_____/_
\ | / /
__ _-----` |{,-----------~
\ }{
}{{
}}{
{{}
, -=-~{ .-^- _
ejm `}
{
EOF
end
end
tree = OrangeTree.new
How to count the correct length of a string with emojis in javascript?
str.length
gives the count of UTF-16 units.Unicode-proof way to get string length in codepoints (in characters) is
[...str].length
as iterable protocol splits the string to codepoints.If we need the length in graphemes (grapheme clusters), we have these native ways:
a. Unicode property escapes in RegExp. See for example: Unicode-aware version of \w or Matching emoji.
b. Intl.Segmenter — coming soon, probably in ES2021. Can be tested with a flag in the last V8 versions (realization was synced with the last spec in V8 86). Unflagged (shipped) in V8 87.
See also:
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
What every JavaScript developer should know about Unicode
JavaScript has a Unicode problem
Unicode-aware regular expressions in ES2015
ES6 Strings (and Unicode, ❤) in Depth
JavaScript for impatient programmers. Unicode – a brief introduction
Related Topics
How to Find Out the Starting Point for Each Match in Ruby
Sqlite3-Ruby Gem Can't Find SQLite3.H on Ubuntu
"The Ruby Way" (Mixins and Class Reopening) VS. Dependency Injection
Emacs Ruby Method Parameter Indentation
How to Get Rufus-Scheduler Working with a Rails App Deployed to Heroku
How to Print Unicode Character U-1F4A9 'Pile of Poo' Emoji
Add External Style Sheet in Rails Project
Finding the Difference Between Strings in Ruby
How to Implement Composite Primary Keys in Rails
How to Send a Parameter to a Before Filter
How to Refresh a Page with Turbolinks
How to Log Specific Request Details to Rails Server Logs
How Does Ruby Handle Array Range Accessing
Rails Render Partial in Helper
How to Return Everything After Last Slash(/) in a Ruby String
Why Does Using '-Webkit-Appearance: None' on a Select Option in Osx Make The Text Disappear